Issue when Manager OwnsObjects = false

Hi,

Need help why this code fails when manager.OwnsObjects := false, I get a invalid pointer exception when calling free to the asociated objects.

This are my entities.

TEMPRESA = class
  private
    ...
   
    [ManyValuedAssociation([TAssociationProp.Lazy], [TCascadeType.SaveUpdate, TCascadeType.Merge, TCascadeType.Remove], 'FIDEMPRESA')]
    FSucursales: Proxy<TList<TSUCURSAL>>;
   
    [ManyValuedAssociation([TAssociationProp.Lazy], [TCascadeType.SaveUpdate, TCascadeType.Merge], 'FIDEMPRESA')]
    FBancos: Proxy<TList<TBANCO>>;
   
    [ManyValuedAssociation([TAssociationProp.Lazy], [TCascadeType.SaveUpdate, TCascadeType.Merge], 'FIDEMPRESA')]
    FCuentasCont: Proxy<TList<TCONTA_CUENTA_EMP>>;
   
    [ManyValuedAssociation([TAssociationProp.Lazy], [TCascadeType.SaveUpdate, TCascadeType.Merge, TCascadeType.Remove], 'FIDEMPRESA')]
    FGlobales: Proxy<TList<TEMPRESA_GLOBAL>>;
   
    [ManyValuedAssociation([TAssociationProp.Lazy], [TCascadeType.SaveUpdate, TCascadeType.Merge, TCascadeType.Remove], 'FIDEMPRESA')]
    FClientes: Proxy<TList<TCLIENTE>>;
    function GetSucursales: TList<TSUCURSAL>;
    function GetBancos: TList<TBANCO>;
    function GetCuentasCont: TList<TCONTA_CUENTA_EMP>;
    function GetGlobales: TList<TEMPRESA_GLOBAL>;
    function GetClientes: TList<TCLIENTE>;
  public
    ...
    property Sucursales: TList<TSUCURSAL> read GetSucursales;
    property Bancos: TList<TBANCO> read GetBancos;
    property CuentasCont: TList<TCONTA_CUENTA_EMP> read GetCuentasCont;
    property Globales: TList<TEMPRESA_GLOBAL> read GetGlobales;
    property Clientes: TList<TCLIENTE> read GetClientes;
  end;

  TCLIENTE_GRUPO = class
  private
    ...
    [Association([TAssociationProp.Lazy], [])]
    [JoinColumn('IDEMPRESA', [], 'ID')]
    FIDEMPRESA: Proxy<TEMPRESA>;
    function GetIDEMPRESA: TEMPRESA;
    procedure SetIDEMPRESA(const Value: TEMPRESA);
  public
    ...
    property IDEMPRESA: TEMPRESA read GetIDEMPRESA write SetIDEMPRESA;
  end;

This is mi code:

function ObtieneGrupos(
  const AIdEmpresa: integer;
  const AIdPadre : integer): TList<TCLIENTE_GRUPO>;
var
  Manager : TObjectManager;
  Trans : IDBTransaction;
  Grupo : TCLIENTE_GRUPO;
  Id : integer;
begin
  Result := nil;
  try
    try
      Manager := GetManager;
      Manager.OwnsObjects := false;
      Trans := StartTransaction;
      Result := Manager.Find<TCLIENTE_GRUPO>
                       .Add(TExpression.Eq('IDEMPRESA', AIdEmpresa))
                       .Add(TExpression.Eq('IDPADRE', AIdPadre))
                       .OrderBy('CODIGO').List;
      for Grupo in Result do
        Id := Grupo.IDEMPRESA.ID;
    except on E:Exception do
      raise;
    end;
  finally
     Trans.Commit;
     FreeAndNil(Manager);
     Trans := nil;
  end;
end;

procedure CargaGrupos;
var
  Grupos : TList<TCLIENTE_GRUPO>;
  Grupo : TCLIENTE_GRUPO;
  Item : TIWCGJQComboBoxExItem;
  i : integer;
  Id : integer;
begin
  Grupos := nil;
  try
    try
      Grupos := ObtieneGrupos(UserSession.IdEmpresa,0);
      for Grupo in Grupos do
        begin
          Item := cbGrupo.Items.Add;
          Item.Caption := Grupo.CODIGO.Value + ' [' + Grupo.NOMBRE.Value + ']';
          Item.Value := IntToStr(Grupo.ID);
          Id := Grupo.IDEMPRESA.ID; // this works ok.
          ...
        end;
    except on E:Exception do
      WebApplication.ShowMessage('ERROR:' + E.Message);
    end;
  finally
    begin
      for i := 0 to Grupos.Count - 1 do
        begin
          Grupos.Items.IDEMPRESA.Free; /// invalid pointer exception.
          Grupos.Items.Free;
        end;
       Grupos.Free;
    end;
  end;
end;

Any hint?

Thanks in Advance,

Omar Zelaya

Hi,
the following code should be read as following(kind of):

Grupos.Items [ i ].IDEMPRESA.Free; /// invalid pointer exception.
Grupos.Items [ i ].Free;

When posting the original message, If I use the correct syntax the editor deletes the array indexing.

I can't see anything wrong. What is the call stack so we know the exact point the error happens in the code?