Avoiding memory leaks

Imagine a master-detail situation with the entities:


TMaster having:
    [ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAll, 'fMaster')]
    fDetails: Proxy<TList<TDetail>>;

TDetail having:
    [Association([TAssociationProp.Lazy], [])]
    [JoinColumn('ID_MASTER', [])] 
    fMaster: Proxy<TMaster>;

and

constructor TMaster.Create;
begin
  inherited Create;
  fDetails.SetInitialValue(TList<TDetail>.Create);
end;

destructor TMaster.Destroy;
begin
  fDetails.DestroyValue;
  inherited Destroy;
end;

This way of creating/destroying fDetails  will not free TDetail instances, resulting in memory leaks.
I think the best way forward would be to declare fDetails as a TObjectList<TDetail> owning the detail instanced.

My question: 
a) what would be the best approach to avoid leaks? Anything that Aurelius does to help?
b) what if the entity-model is rather complex with N-M relations between the details of different master-detail sets?

Appreciate your inputs!

 

That is correct and that's how it's supposed to be.

Usually your entities will be managed by Aurelius TObjectManager. The entities will be retrieved from the database and the manager will keep track of the references, when the manager is destroyed, the managed entities will be destroyed. Having the objects to control lifetime of associations is not desired at all, since you can have the same object being referenced by many other objects (i.e., a single instance object TCountry can be referenced my many instanced of TCustomer objects for example).