Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 6

Qt-interest Archive, February 2003
QT & Destructors

Pages: Prev | 1 | 2 | Next

Message 1 in thread

Greetings All,

I've noticed that in my apps, the Mainform::destroy() does not appear to be
called when the application is closed.  Has anyone noticed this?

Also, I have classes (derived from QT classes) that have destructors.  Those
destructors do not appear to be called either.

Any ideas on how to clearly show that the destructors are being called?

Thanks,
Thom Wharton

Thomas Wharton
Software Engineer
Raytheon Co.
757-421-8794


Message 2 in thread

On Thu, 13 Feb 2003, Thomas Wharton wrote:

> Greetings All,
> 
> I've noticed that in my apps, the Mainform::destroy() does not appear to be
> called when the application is closed.  Has anyone noticed this?
> 
> Also, I have classes (derived from QT classes) that have destructors.  Those
> destructors do not appear to be called either.
> 
> Any ideas on how to clearly show that the destructors are being called?
> 
You may run your program through a debugger and set breakpoints in the
destructors in question.

	Daniel


Message 3 in thread

On Thursday 13 February 2003 15:54, Thomas Wharton wrote:
> Any ideas on how to clearly show that the destructors are being called?

a debugger with breakpoints, perhaps?

HTH

Johannes Lochmann

-- 
 [ signature omitted ] 

Message 4 in thread

Thomas Wharton wrote:
> 
> Greetings All,
> 
> I've noticed that in my apps, the Mainform::destroy() does not appear to be
> called when the application is closed.  Has anyone noticed this?

Yes, me! I already posted this a few days ago (7.Feb.: "destroy never
called?") but didn't get any response. Glad that I'm not alone(tm) ;)

> Also, I have classes (derived from QT classes) that have destructors.  Those
> destructors do not appear to be called either.

Hmmm, just a guess: if you call qDebug() in your d'tor it might be that
at that point the output stream is already disconnected (by Qt) and you
won't get any console output anymore (in other words, your d'tor is
still being called, but you won't notice it since no console output),
but am very not sure on this.

> 
> Any ideas on how to clearly show that the destructors are being called?

I didn't go too much into this (in my destroy() case I just put
everything into the closeEvent() method of my main window instead, as a
workaround...) but would be interested, too.

Cheers, Oliver


Message 5 in thread

On Thursday 13 February 2003 15:54, Thomas Wharton wrote:
> Greetings All,
>
> I've noticed that in my apps, the Mainform::destroy() does not appear to be
> called when the application is closed.  Has anyone noticed this?
>
> Also, I have classes (derived from QT classes) that have destructors. 
> Those destructors do not appear to be called either.
>
> Any ideas on how to clearly show that the destructors are being called?
>
> Thanks,
> Thom Wharton
>
> Thomas Wharton
> Software Engineer
> Raytheon Co.
> 757-421-8794
>
> --
> List archive and information: http://lists.trolltech.com/qt-interest/
I personally like the 
cout << "Inside destructor of " << name() << endl;

Happy coding,
Eric


Message 6 in thread

Heya,

I got a few responses -- thank you.  However, I should have clarified that I
am using gdb and I am not seeing the destructors called when the app exits.
I also have put in cout statements in each destructor to see if it is
called.

Still a bit clueless here -- though I have learned that when using
QT-Designer, if you want to see it's destructor called you have to override
the virtual destroy(bool, bool) from QWidget.  This seems a bit weird to me,
because the comments at the top of mainform.ui.h say to create a slot called
destroy(). If you do that, in the Source window you will see the function
destroy() with the word 'destructor' next to it (a warm fuzzy).  However, if
you replace destroy() with destroy(bool, bool) the word 'destructor' does
not appear.

I know I've read in the docs that QT takes care of deleting child objects,
but I am not sure if that rule extends to my derived classes (and if it
does, than why am I not seeing my cout statements when the app terminates,
or hitting a breakpoint on my classes destructor in gdb?)

Anyone?

Thoroughly Confounded :)
Thom Wharton

>> -----Original Message-----
>> From: owner-qt-interest@trolltech.com
>> [mailto:owner-qt-interest@trolltech.com]On Behalf Of Thomas Wharton
>> Sent: Thursday, February 13, 2003 9:54 AM
>> To: qt-interest@trolltech.com
>> Subject: QT & Destructors
>>
>>
>> Greetings All,
>>
>> I've noticed that in my apps, the Mainform::destroy() does not
>> appear to be
>> called when the application is closed.  Has anyone noticed this?
>>
>> Also, I have classes (derived from QT classes) that have
>> destructors.  Those
>> destructors do not appear to be called either.
>>
>> Any ideas on how to clearly show that the destructors are being called?
>>
>> Thanks,
>> Thom Wharton
>>
>> Thomas Wharton
>> Software Engineer
>> Raytheon Co.
>> 757-421-8794
>>
>> --
>> List archive and information: http://lists.trolltech.com/qt-interest/


Message 7 in thread

Thomas Wharton wrote:
> 
> Heya,
> 
> Still a bit clueless here -- though I have learned that when using
> QT-Designer, if you want to see it's destructor called you have to override
> the virtual destroy(bool, bool) from QWidget.  This seems a bit weird to me,
> because the comments at the top of mainform.ui.h say to create a slot called
> destroy(). If you do that, in the Source window you will see the function
> destroy() with the word 'destructor' next to it (a warm fuzzy).  However, if
> you replace destroy() with destroy(bool, bool) the word 'destructor' does
> not appear.

So is it destroy (bool, bool) or destroy()? I'm in the very same
situation here...

Btw I implemented destroy() as a method, not slot. That is I simply
started typing

  MyApplicationWindow::destroy() {...}

into the source file, therefore I didn't see the word 'destructor'... As
for the functionality this shouldn't make any difference anyway.

But see my other mail I just sent, about the main() function...

Cheers, Oliver


Message 8 in thread

I have thought a little about the problem and I think the explanation is
pretty obvious:

destroy() is called in the last line of the QWidget destructor. At this
time, all subclass constructors have already completed.
Do you see what I mean? At this time, the instance is only a QWidget
instance, no matter what it was before! Thus only QWidget::destroy() is
called.

If you want your overloaded method to be called, you must call it
yourself.

Do you get my point?

	Daniel

On Thu, 13 Feb 2003, Till Oliver Knoll wrote:

> Thomas Wharton wrote:
> > 
> > Heya,
> > 
> > Still a bit clueless here -- though I have learned that when using
> > QT-Designer, if you want to see it's destructor called you have to override
> > the virtual destroy(bool, bool) from QWidget.  This seems a bit weird to me,
> > because the comments at the top of mainform.ui.h say to create a slot called
> > destroy(). If you do that, in the Source window you will see the function
> > destroy() with the word 'destructor' next to it (a warm fuzzy).  However, if
> > you replace destroy() with destroy(bool, bool) the word 'destructor' does
> > not appear.
> 
> So is it destroy (bool, bool) or destroy()? I'm in the very same
> situation here...
> 
> Btw I implemented destroy() as a method, not slot. That is I simply
> started typing
> 
>   MyApplicationWindow::destroy() {...}
> 
> into the source file, therefore I didn't see the word 'destructor'... As
> for the functionality this shouldn't make any difference anyway.
> 
> But see my other mail I just sent, about the main() function...
> 
> Cheers, Oliver
> 
> --
> List archive and information: http://lists.trolltech.com/qt-interest/
> 


Message 9 in thread

On Thursday 13 February 2003 17:55, Daniel Junglas wrote:
> I have thought a little about the problem and I think the explanation is
> pretty obvious:
>
> destroy() is called in the last line of the QWidget destructor. At this
> time, all subclass constructors have already completed.
I'd hope so; after all I usually create a window quite some time before it's 
being deleted. Where does this misquote come from?
> Do you see what I mean? At this time, the instance is only a QWidget
> instance, no matter what it was before! Thus only QWidget::destroy() is
> called.
No. Not right.
Conisder this class hierarchy:
class A;
class B: A;
class C: B;

construction of a 'new' C will proceed as follows:
first A gets created
then B gets created
finally C gets created.

Destruction happens the other way around; so 
first C's destructor invoked
then B's destructor invoked
finally A's destructor invoked

If each destructor had a cout, you would see them in this order.
(I'll ignore the correct type/virtual thing here; QObject's destructor is 
virtual after all).

Problem here is still the mainWIdget: here same thing happens as in other 
thread: It never get's deleted. If it does not get deleted, the destroy will 
not be emitted, hence the destroy -or whatever you are waiting or- will never 
be called.

>
> If you want your overloaded method to be called, you must call it
> yourself.
>
> Do you get my point?
>
> 	Daniel
>
> On Thu, 13 Feb 2003, Till Oliver Knoll wrote:
> > Thomas Wharton wrote:
> > > Heya,
> > >
> > > Still a bit clueless here -- though I have learned that when using
> > > QT-Designer, if you want to see it's destructor called you have to
> > > override the virtual destroy(bool, bool) from QWidget.  This seems a
> > > bit weird to me, because the comments at the top of mainform.ui.h say
> > > to create a slot called destroy(). If you do that, in the Source window
> > > you will see the function destroy() with the word 'destructor' next to
> > > it (a warm fuzzy).  However, if you replace destroy() with
> > > destroy(bool, bool) the word 'destructor' does not appear.
> >
> > So is it destroy (bool, bool) or destroy()? I'm in the very same
> > situation here...
> >
> > Btw I implemented destroy() as a method, not slot. That is I simply
> > started typing
> >
> >   MyApplicationWindow::destroy() {...}
> >
> > into the source file, therefore I didn't see the word 'destructor'... As
> > for the functionality this shouldn't make any difference anyway.
> >
> > But see my other mail I just sent, about the main() function...
> >
> > Cheers, Oliver
> >
> > --
> > List archive and information: http://lists.trolltech.com/qt-interest/
>
> --
> List archive and information: http://lists.trolltech.com/qt-interest/


Message 10 in thread

> I've noticed that in my apps, the Mainform::destroy() does not appear to be
> called when the application is closed.  Has anyone noticed this?

Why do you expect it to be called when you're never deleting the object 
instance in question????

Nobody (as in no part of Qt) will do it for you unless you ask for it ;-)))

Cheers, Kuba Ober


Message 11 in thread

<QUOTE>

destroy() is called in the last line of the QWidget destructor. At this time, all subclass constructors have already completed.  Do you see what I mean? At this time, the instance is only a QWidget instance, no matter what it was before! Thus only QWidget::destroy() is called.

</QUOTE>

I'm not sure I understand why the derived class destructor would not be called, as suggested by this statement.  In my experience, unless the destructor is declared virtual, ONLY the derived class destructor is called.  If you declare the destructor as virtual, the entire chain is called.
 
If you're deriving from your own class and expecting the destructor to be called, are you declaring your destructor virtual?
 
Kevin

Message 12 in thread

Hello,

I've solved one mystery.  Using QT-Designer, you get a generated form1.ui.h file which is the source code for your derived class from some QT base class (like a MainWindow or Dialog).  The notes at the top of the file say if you want a destructor, create a destroy() function.  It should read, create a destroy(bool win, bool sub) function.  I got this after looking over qwidget.h in the QT source directory.  qwidget.h declares a protected virtual function called destroy(...) that takes two bool values that have defaults (hence the confusion of declaring it as destroy()in the QT-Designer derived class verse (the correct) destroy(bool, bool).

Now I have gotten that to work, and I can see that my form's destructor is called.  However, this still leaves me with the other problem...

In my form's class header, I have a declared a pointer (pPlot) to the class ThomPlot(a class derived from QwtPlot, which in turn is derived from a QT object).

  ThomPlot* pPlot;

In the form's init() function, I use new to create an object of class QwtPlot.

  pPlot = new ThomPlot();

In the source files for ThomPlot I have a destructor with a cout statement.  

Now, at first I assumed the QT would clean up the memory I allocated in the forms init() (I've read some documentation that says as much).  However, I've never seen the result of the cout in ThomPlot's destructor.  Also, though I have set a breakpoint on the destructor in ThomPlot using gdb, it does not appear to be reached when exiting the application.

So, from that I guessed I would have to use delete on pPlot in the form's destructor.  If I do that, I do see the result of the cout statement from ~ThomPlot().  However, when the program hits the ending brace of the forms destructor, I get a segmentation fault.  Has anybody actually experienced, or can verify this?

Thanks again for the all the input on this.  



>> -----Original Message-----
>> From: owner-qt-interest@trolltech.com
>> [mailto:owner-qt-interest@trolltech.com]On Behalf Of Fisk, Kevin
>> Sent: Thursday, February 13, 2003 12:16 PM
>> To: qt-interest@trolltech.com
>> Subject: Re: QT & Destructors
>> 
>> 
>> <QUOTE>
>> 
>> destroy() is called in the last line of the QWidget destructor. 
>> At this time, all subclass constructors have already completed.  
>> Do you see what I mean? At this time, the instance is only a 
>> QWidget instance, no matter what it was before! Thus only 
>> QWidget::destroy() is called.
>> 
>> </QUOTE>
>> 
>> I'm not sure I understand why the derived class destructor would 
>> not be called, as suggested by this statement.  In my 
>> experience, unless the destructor is declared virtual, ONLY the 
>> derived class destructor is called.  If you declare the 
>> destructor as virtual, the entire chain is called.
>>  
>> If you're deriving from your own class and expecting the 
>> destructor to be called, are you declaring your destructor virtual?
>>  
>> Kevin
>> .+-j!?~jml6%?&b?z
>> 


Message 13 in thread

Thomas Wharton wrote:

> 
> In the form's init() function, I use new to create an object of class QwtPlot.
> 
>   pPlot = new ThomPlot();
> 
> In the source files for ThomPlot I have a destructor with a cout statement.
> 
> Now, at first I assumed the QT would clean up the memory I allocated in the forms init() (I've read some documentation that says as much).  However, I've never seen the result of the cout in ThomPlot's destructor. 

Which documentation was this? Just to make things clear: Qt only cleans
stuff after you (roughly speaking), if you allocate a QWidget with a
*parent* which takes care of it's children. Allocating memory (which Qt
doesn't know of) in this init() method doesn't mean you can forget about
it, you have to delete it yourself, as in:

MyWidget::init() {

  // is deleted by Qt as soon this QWidget is deleted
  QWidget *w1 = new QWidget (this);

  // _you_ have to delete w2
  QWidget *w2 = new QWidget();

  // _you_ have to delete data
  int data = new data[32];

  // etc.

}


Message 14 in thread

Opps, meant to send this to whole list...

-----Original Message-----
From: Paul St. John [mailto:pstjohn@igotechnologies.com]
Sent: 13/02/2003 11:27 AM
To: thomas_wharton@rothr.rsc.raytheon.com
Subject: RE: QT & Destructors


Thom,

Destructors of derived classes will not be called if an
object is deleted using a pointer or referece to the
base.  Could it be that this is what you're seeing?
if so, check out 'virtual destructors'.

Paul St. John
www.visible-bytes.com

-----Original Message-----
From: owner-qt-interest@trolltech.com
[mailto:owner-qt-interest@trolltech.com]On Behalf Of Thomas Wharton
Sent: 13/02/2003 11:13 AM
To: qt-interest@trolltech.com
Subject: RE: QT & Destructors


Heya,

I got a few responses -- thank you.  However, I should have clarified that I
am using gdb and I am not seeing the destructors called when the app exits.
I also have put in cout statements in each destructor to see if it is
called.

Still a bit clueless here -- though I have learned that when using
QT-Designer, if you want to see it's destructor called you have to override
the virtual destroy(bool, bool) from QWidget.  This seems a bit weird to me,
because the comments at the top of mainform.ui.h say to create a slot called
destroy(). If you do that, in the Source window you will see the function
destroy() with the word 'destructor' next to it (a warm fuzzy).  However, if
you replace destroy() with destroy(bool, bool) the word 'destructor' does
not appear.

I know I've read in the docs that QT takes care of deleting child objects,
but I am not sure if that rule extends to my derived classes (and if it
does, than why am I not seeing my cout statements when the app terminates,
or hitting a breakpoint on my classes destructor in gdb?)

Anyone?

Thoroughly Confounded :)
Thom Wharton

>> -----Original Message-----
>> From: owner-qt-interest@trolltech.com
>> [mailto:owner-qt-interest@trolltech.com]On Behalf Of Thomas Wharton
>> Sent: Thursday, February 13, 2003 9:54 AM
>> To: qt-interest@trolltech.com
>> Subject: QT & Destructors
>>
>>
>> Greetings All,
>>
>> I've noticed that in my apps, the Mainform::destroy() does not
>> appear to be
>> called when the application is closed.  Has anyone noticed this?
>>
>> Also, I have classes (derived from QT classes) that have
>> destructors.  Those
>> destructors do not appear to be called either.
>>
>> Any ideas on how to clearly show that the destructors are being called?
>>
>> Thanks,
>> Thom Wharton
>>
>> Thomas Wharton
>> Software Engineer
>> Raytheon Co.
>> 757-421-8794
>>
>> --
>> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 15 in thread

If the class is void, that's true.
 
If you do:
 
QFoo* test = new QFoo();
delete test;
 
it will be called.  If you do:
 
QFoo* test = new QFoo();
void *temp = (void*) test;
delete temp;
 
As you might if you pass the class as a void pointer, then it won't be called.
 
delete (QFoo*) temp;
 
will then cause the destructor to be called.
 
Kevin
 

	-----Original Message----- 
	From: Paul St. John [mailto:pstjohn@igotechnologies.com] 
	Sent: Thu 2/13/2003 9:21 AM 
	To: qt-interest@trolltech.com 
	Cc: 
	Subject: FW: QT & Destructors
	
	

	Opps, meant to send this to whole list...
	
	-----Original Message-----
	From: Paul St. John [mailto:pstjohn@igotechnologies.com]
	Sent: 13/02/2003 11:27 AM
	To: thomas_wharton@rothr.rsc.raytheon.com
	Subject: RE: QT & Destructors
	
	
	Thom,
	
	Destructors of derived classes will not be called if an
	object is deleted using a pointer or referece to the
	base.  Could it be that this is what you're seeing?
	if so, check out 'virtual destructors'.
	
	Paul St. John
	www.visible-bytes.com
	
	-----Original Message-----
	From: owner-qt-interest@trolltech.com
	[mailto:owner-qt-interest@trolltech.com]On Behalf Of Thomas Wharton
	Sent: 13/02/2003 11:13 AM
	To: qt-interest@trolltech.com
	Subject: RE: QT & Destructors
	
	
	Heya,
	
	I got a few responses -- thank you.  However, I should have clarified that I
	am using gdb and I am not seeing the destructors called when the app exits.
	I also have put in cout statements in each destructor to see if it is
	called.
	
	Still a bit clueless here -- though I have learned that when using
	QT-Designer, if you want to see it's destructor called you have to override
	the virtual destroy(bool, bool) from QWidget.  This seems a bit weird to me,
	because the comments at the top of mainform.ui.h say to create a slot called
	destroy(). If you do that, in the Source window you will see the function
	destroy() with the word 'destructor' next to it (a warm fuzzy).  However, if
	you replace destroy() with destroy(bool, bool) the word 'destructor' does
	not appear.
	
	I know I've read in the docs that QT takes care of deleting child objects,
	but I am not sure if that rule extends to my derived classes (and if it
	does, than why am I not seeing my cout statements when the app terminates,
	or hitting a breakpoint on my classes destructor in gdb?)
	
	Anyone?
	
	Thoroughly Confounded :)
	Thom Wharton
	
	>> -----Original Message-----
	>> From: owner-qt-interest@trolltech.com
	>> [mailto:owner-qt-interest@trolltech.com]On Behalf Of Thomas Wharton
	>> Sent: Thursday, February 13, 2003 9:54 AM
	>> To: qt-interest@trolltech.com
	>> Subject: QT & Destructors
	>>
	>>
	>> Greetings All,
	>>
	>> I've noticed that in my apps, the Mainform::destroy() does not
	>> appear to be
	>> called when the application is closed.  Has anyone noticed this?
	>>
	>> Also, I have classes (derived from QT classes) that have
	>> destructors.  Those
	>> destructors do not appear to be called either.
	>>
	>> Any ideas on how to clearly show that the destructors are being called?
	>>
	>> Thanks,
	>> Thom Wharton
	>>
	>> Thomas Wharton
	>> Software Engineer
	>> Raytheon Co.
	>> 757-421-8794
	>>
	>> --
	>> List archive and information: http://lists.trolltech.com/qt-interest/
	
	--
	List archive and information: http://lists.trolltech.com/qt-interest/
	---
	Incoming mail is certified Virus Free.
	Checked by AVG anti-virus system (http://www.grisoft.com).
	Version: 6.0.445 / Virus Database: 250 - Release Date: 21/01/2003
	
	---
	Outgoing mail is certified Virus Free.
	Checked by AVG anti-virus system (http://www.grisoft.com).
	Version: 6.0.445 / Virus Database: 250 - Release Date: 21/01/2003
	
	---
	Outgoing mail is certified Virus Free.
	Checked by AVG anti-virus system (http://www.grisoft.com).
	Version: 6.0.445 / Virus Database: 250 - Release Date: 21/01/2003
	
	--
	List archive and information: http://lists.trolltech.com/qt-interest/
	
	


Pages: Prev | 1 | 2 | Next