Board index » delphi » Preventing of double calling of TActiveForm destructor ??

Preventing of double calling of TActiveForm destructor ??

Hi,

Here is a small problem I have:

I have an ActiveForm with a child interface.

TMyActiveForm = class (TActiveForm, IMyActiveForm)
private
  Child: IChild;
protected
  ...
  procedure FormMethode; stdcall;
public
  ..
  destructor Destroy; override;
end;

Child interface uses loose pointer to it's parent (TMyActiveForm) to avoid
circular referencing.

TChild = class (TAutoIntfObject, IChild)
private
  FParent: Pointer; // loose pointer to parent interface IMyActiveForm
  ..
public
  ..
  destructor Destroy; override;
end;

On ActiveForm destruction I'm also free the child object:

destructor TMyActiveForm.Destroy;
begin
  Child := nil;
  ...
  ReleaseOtherData;
  inherited;
end;

Nothing spectacular so far.
But here is the problem:

In child's destructor I have to use a method from the MyActiveForm to free
some data.
At the moment of calling child destructor's the reference count of
TMyActiveForm is null. It becomes 1 and then again 0, while I'm calling
FormMethode. That means on RefCount = 0 Delphi calls TMyActiveForm.Destroy
again! (See the code and comments)

destructor TChild.Destroy;
var
  MyActiveForm: IMyActiveForm;
begin

  // TMyActiveForm.RefCount = 0
  MyActiveForm := IMyActiveForm(FParent);
  // now TMyActiveForm.RefCount = 1
  MyActiveForm.FormMethode;
  MyActiveForm := nil;
  // now TMyActiveForm.RefCount = 0 again

  inherited;   // -> TMyActiveForm Destructor is called again
end;

Is there a way to prevent Delphi calling the Destructor second time???
I can use a kind of flag for this, but is there the "right way" to solve the
problem?

Thanks a lot,
Alex

 

Re:Preventing of double calling of TActiveForm destructor ??


Hi,

Here is a small problem I have:

I have an ActiveForm with a child interface.

TMyActiveForm = class (TActiveForm, IMyActiveForm)
private
  Child: IChild;
protected
  ...
  procedure FormMethode; stdcall;
public
  ..
  destructor Destroy; override;
end;

Child interface uses loose pointer to it's parent (TMyActiveForm) to avoid
circular referencing.

TChild = class (TAutoIntfObject, IChild)
private
  FParent: Pointer; // loose pointer to parent interface IMyActiveForm
  ..
public
  ..
  destructor Destroy; override;
end;

On ActiveForm destruction I'm also free the child object:

destructor TMyActiveForm.Destroy;
begin
  Child := nil;
  ...
  ReleaseOtherData;
  inherited;
end;

Nothing spectacular so far.
But here is the problem:

In child's destructor I have to use a method from the MyActiveForm to free
some data.
At the moment of calling child destructor's the reference count of
TMyActiveForm is null. It becomes 1 and then again 0, while I'm calling
FormMethode. That means on RefCount = 0 Delphi calls TMyActiveForm.Destroy
again! (See the code and comments)

destructor TChild.Destroy;
var
  MyActiveForm: IMyActiveForm;
begin

  // TMyActiveForm.RefCount = 0
  MyActiveForm := IMyActiveForm(FParent);
  // now TMyActiveForm.RefCount = 1
  MyActiveForm.FormMethode;
  MyActiveForm := nil;
  // now TMyActiveForm.RefCount = 0 again

  inherited;   // -> TMyActiveForm Destructor is called again
end;

Is there a way to prevent Delphi calling the Destructor second time???
I can use a kind of flag for this, but is there the "right way" to solve the
problem?

Thanks a lot,
Alex

Other Threads