Freeing a not used object

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.. :slight_smile:

Easier way is to tell the manager to destroy the object no matter what. This is described here: Manipulating Objects | TMS Aurelius documentation

In other words, whenever you create an instance of an object that might be eventually destroyed by the manager (like in the Save operation you mentioned), then it's easier just to call AddOwnership right away. This will tell the manager to destroy the object no matter what happens (even if the object is not saved).

From the doc:

Customer := TTC_Customer.Create;
Customer.Name := 'Customer Name';

// Tell the manager to destroy Customer
ObjectManager1.AddOwnership(Customer);

if OkButtonClicked then
  ObjectManager1.Save(Customer);

// Eventually Customer will be destroyed when manager is destroyed
// even if Save is not called

Great, thank you!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.