<div dir="ltr"><div><div>Hello,<br><br></div>I have found a bug in the TInterfacedObject.<br><br><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">Better than explaining is to examine and execute the code below. </span></span><br><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">When executing the code below the destructor of the class TCustomDestructorClass is called infinitely. </span></span><br><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">This is due to calling the procedure Foo inside the destructor of TCustomDestructorClass, wh</span></span><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">hich causes the _addRef and _Release methods to be called again.</span></span><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-"> As the reference count reaches zero again, this triggers the calling of the destructor again e and </span></span><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-">so the cycle will repeat infinitely.<br><br></span></span><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">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). </span></span></span></span><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-"><span id="gmail-result_box" class="gmail-" lang="en"><span class="gmail-">Currently the method only checks if the reference count</span></span> reaches zero.<br><br></span></span></span></span></div><div><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-">==== CODE BEGIN =========<br><br>program interfaced_object_error_2;<br><br>type<br><br> { TCustomDeestructorClass }<br><br> TCustomDestructorClass = class(TInterfacedObject)<br> public<br> destructor Destroy; override;<br> end;<br><br>var<br> DestrcutorCount: Integer = 0;<br> CustomRef: IInterface;<br><br>procedure Foo(Value: IInterface);<br>begin<br> if Value <> nil then<br> Inc(DestrcutorCount);<br>end;<br><br>{ TCustomDeestructorClass }<br><br>destructor TCustomDestructorClass.Destroy;<br>begin<br> Foo(Self);<br> inherited Destroy;<br>end;<br><br>begin<br> CustomRef := TCustomDestructorClass.Create;<br> CustomRef := TCustomDestructorClass.Create; // Forces execution of destructor<br>end.<br><br></span></span></div><div><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-">=== END ===========<br></span></span></div><div><span id="gmail-result_box" class="gmail-short_text" lang="en"><span class="gmail-"><br></span></span></div><div><br></div><div><span id="gmail-result_box" class="gmail-" lang="en"><span></span></span><br></div><div><span id="gmail-result_box" class="gmail-" lang="en"><span></span></span></div></div>