Xojo Conferences
MBSOct2019CologneDE

Destructor called when? (Real Studio network user group Mailinglist archive)

Back to the thread list
Previous thread: Peer to Peer Messaging
Next thread: Yahoo Messager Sign on Sequence


Re: recordset's recordcount   -   Jan Erik Moström <
  Destructor called when?   -   Christian Schmitz
   Re: Destructor called when?   -   Joseph J. Strout

Destructor called when?
Date: 27.08.02 23:24 (Wed, 28 Aug 2002 00:24:57 +0200)
From: Christian Schmitz
Hi,

I've just made a small testing project.

It looks like the destructor is not called directly, but some time
later.

Just two classes (class1 and class2) with properties:

property window1.c as class1
property class1.d as class2

Window1.Close:
Sub Close()
c=nil

if c=nil then
msgBox "c is now nil."
else
msgBox "c is not nil."
end if
End Sub

Window1.Open:
Sub Open()

c=new class1
End Sub

Class1.class1:
Sub class1()
d=new class2
End Sub

Class1.~Class1:
Sub ~Class1()

d=nil
msgBox "class1 destructor done."
End Sub

Class2.~Class2:
Sub ~Class2()
msgBox "class2 destructor done."
End Sub

Now I set the reference to d to nil, but the detructor of class2 is
called later, so I may get in trouble.

And I'm currently with one project in real trouble because the
destruction is not done in the correct order.

Mfg
Christian

Realbugs report # 87240.

Re: Destructor called when?
Date: 27.08.02 23:30 (Tue, 27 Aug 2002 15:30:49 -0700)
From: Joseph J. Strout
At 12:24 AM +0200 8/28/02, Christian Schmitz wrote:

>It looks like the destructor is not called directly, but some time
>later.

It's called when the last reference to the object is removed --
immediately, unless there are already objects in the process of being
destroyed (those finish first).

>Just two classes (class1 and class2) with properties:
>
>property window1.c as class1
>property class1.d as class2
>
>Window1.Close:
>Sub Close()
> c=nil
>
> if c=nil then
> msgBox "c is now nil."
> else
> msgBox "c is not nil."
> end if
>End Sub
>
>Window1.Open:
>Sub Open()
>
> c=new class1
>End Sub
>
>Class1.class1:
>Sub class1()
> d=new class2
>End Sub
>
>Class1.~Class1:
>Sub ~Class1()
>
> d=nil
> msgBox "class1 destructor done."
>End Sub
>
>Class2.~Class2:
>Sub ~Class2()
> msgBox "class2 destructor done."
>End Sub
>
>Now I set the reference to d to nil, but the detructor of class2 is
>called later, so I may get in trouble.

Sorry, I can't quite see what you mean. When you set class1.d to
nil, if that was the last reference to that object, then the class2
destructor will be called immediately or as soon as all other
condemned objects have finished being destroyed.

>And I'm currently with one project in real trouble because the
>destruction is not done in the correct order.

If you have a chain of objects all being destroyed at once, the order
is undefined. So there is no "correct" order, and you shouldn't
write code that depends on any particular order.

Cheers,
- Joe