TInterfacedObject destructor reentrance bug

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

TInterfacedObject destructor reentrance bug

African Wild Dog
Hello,

I have found a bug in the TInterfacedObject.

Better than explaining is to examine and execute the code below.
When executing the code below the destructor of the class TCustomDestructorClass is called infinitely.
This is due to calling the procedure Foo inside the destructor of TCustomDestructorClass, whhich causes the _addRef and _Release methods to be called again. As the reference count reaches zero again, this triggers the calling of the destructor again e and so the cycle will repeat infinitely.

I think one solution would be the _Release method to check if the instance is already being destroyed before calling the destructor of the class (Destroy method). Currently the method only checks if the reference count reaches zero.

==== CODE BEGIN =========

program interfaced_object_error_2;

type

  { TCustomDeestructorClass }

  TCustomDestructorClass = class(TInterfacedObject)
  public
    destructor Destroy; override;
  end;

var
  DestrcutorCount: Integer = 0;
  CustomRef: IInterface;

procedure Foo(Value: IInterface);
begin
  if Value <> nil then
    Inc(DestrcutorCount);
end;

{ TCustomDeestructorClass }

destructor TCustomDestructorClass.Destroy;
begin
  Foo(Self);
  inherited Destroy;
end;

begin
  CustomRef := TCustomDestructorClass.Create;
  CustomRef := TCustomDestructorClass.Create; // Forces execution of destructor
end.

=== END ===========




_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TInterfacedObject destructor reentrance bug

Michael Van Canneyt


On Sat, 15 Jul 2017, African Wild Dog wrote:

> Hello,
>
> I have found a bug in the TInterfacedObject.

Please report it in the bugtracker, so it won't be forgotten.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: TInterfacedObject destructor reentrance bug

African Wild Dog

2017-07-16 6:03 GMT-03:00 Michael Van Canneyt <[hidden email]>:


Please report it in the bugtracker, so it won't be forgotten.

Michael.


Regards


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Loading...