Invalid pointer operation when releasing TObjectManager in base form

When getting the data for my dialog box, I use the follow code. The idea is to create a new TRevenue entity and edit it in the adsRevenue dataset. When closing the dialog with OK, the entity must be persisted to the database and then the grid calling on the form is refreshed to include/show the new entity.:

  inherited;
  FObjMgr.EnableFilter('LedgerRevenue');

  adsRevenue.Manager := FObjMgr;
  adsAdmin.Manager := FObjMgr;

  // All ads... = TAureliusDataSet

  LAdmin := FObjMgr.Find<TAdministration>(ADataID);
  adsAdmin.SetSourceObject(LAdmin); 
  adsAdmin.Open;

  adsRevenue.SetSourceList(TObjectList<TBooking>.Create(true), true);
  adsRevenue.Open;
  adsRevenue.Append;

Note that FObjMgr is created/destroyed in the base form (TfrmBase) from which this dialog box is inherited.

When closing the form, I execute the following:

  inherited;
  if adsRevenue.Active then
  begin
    if ModalResult = mrOK then
      adsRevenue.Post
    else
      adsRevenue.Cancel;
    adsRevenue.Close;
  end;

  if adsAdmin.Active then
    adsAdmin.Close;


  adsAdmin.Manager := nil;
  adsRevenue.Manager := nil;

When I free the dialog box (in the calling form), I get an 'Invalid Pointer Operation' exception when calling FObjMgr.Free in the base form's destrructor (TfrmBase.Destroy). To be precisely, when FOwnership.Free is called in TObjectManager.Destroy (Aurelius.Engine.ObjectManager). Note that the new entity is correctly saved to the database!

As this problem does not occur with the other forms derived from TfrmBase, I am suspecting it's caused by setting the SourceList for the adsRevenue dataset. However, I cannot figure out what I am doing wrong precisely.

Any thoughts or debugging suggestions you can give me?

Correct. This is what you should use:

  adsRevenue.SetSourceList(TObjectList<TBooking>.Create(false), true);

In your previous code, when the list is destroyed, it would destroy its TBooking objects as well. But those objects are being managed (and eventually destroyed) by the manager. So, the list should just be a container but not manage (destroy) its item objects, hence the use of false in the Create parameter.

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