Hello!
I have a situation that I thing is common, but don't know how to hasndle the leaking. In the program is a part where ther user open a new item - tihis is done by creating a new object and then passing it to the dialog window in the create method. The dialog has a Save button (that saves the product via a ObjectManager) and a Cancel buttons that basically frees the unused objects (so it's not used by the ObjectManager).
Here's a simplified example of classes.
TProduct has a property ProductType and OnStock (a list), there are two types of property just for easier explanation.
[Entity]
[Id('FId', TIdGenerator.IdentityOrSequence)]
TProductStock = class(TBaseObject)
private
[Column('ID')]
FId: integer;
[Association([TAssociationProp.Lazy], CascadeTypeAllButRemove)]
[JoinColumn('PROD_ID', [], 'ID')]
FProduct: Proxy<TProduct>;
[Column('STOCK')]
FOnStock: double;
function GetId: TProduct;
procedure SetId(const Value: TProduct);
public
property Id: integer read FId write FId;
property Product: TProduct read GetId write SetId;
property OnStock: double read FOnStock write FOnStock;
end;
[Entity]
[Id('FId', TIdGenerator.IdentityOrSequence)]
TProductType = class(TBaseObject)
private
[Column('ID')]
FId: integer;
[Column('DESCRIPTION')]
FDescription: string;
public
property Id: integer read FId write FId;
property Description: string read FDescription write FDescription;
end;
[Entity]
[Id('FId', TIdGenerator.IdentityOrSequence)]
TProduct = class(TBaseObject)
private
[Column('ID')]
FId: integer;
[Association([TAssociationProp.Lazy], CascadeTypeAllButRemove)]
[JoinColumn('TYPE_ID', [])]
FProductType: Proxy<TProductType>;
[Column('DESCRIPTION')]
FDescription: string;
[ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAllRemoveOrphan, 'FProduct')]
FStocks: Proxy<TList<TProductStock>>;
public
public
constructor Create; reintroduce;
destructor Destroy; override;
property Id: integer read FId write FId;
property ProductType: TProductType read GetProductType write SetProductType;
property Description: string read FDescription write FDescription;
property Stocks: TProductStock read GetStocks write SetStocks;
end;
constructor TProduct.Create;
begin
inherited Create;
FStocks.SetInitialValue(TList<TProductStock>.Create);
end;
destructor TProduct.Destroy;
begin
try
FStocks.DestroyValue;
finally
inherited;
end;
end;
My question is how to properly free the unused TProduct if the user clicks the cancel button. If I just execute Product.Free, then the properties ProductType and Stocks are not freed (there is no explicit freeing in the Destroy method) and I have a leak.
In the Aurelius manual is stated that we do not have to free, the manager will do this. But since we do not use the manager in thih scenario and just want to free the object, do we need to manually free all the properties? Ot I must somehow add the freeing in the Destroy method?
Hope I am clear, because my english is sometimes clumsy..