Results 1 to 13 of 13

Thread: Destructor not being called

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Destructor not being called

    Hello,

    My destructor is not being called. Can anyone tell me why ?

    .h file:
    Qt Code:
    1. class TransactionDialog : public QDialog {
    2. Q_OBJECT
    3. Q_DISABLE_COPY(TransactionDialog)
    4. public:
    5. // explicit TransactionDialog(QWidget *parent = 0);
    6. explicit TransactionDialog(QWidget *parent = 0, int anID=0, int aType=0);
    7. virtual ~TransactionDialog();
    8.  
    9. protected:
    10. virtual void changeEvent(QEvent *e);
    11.  
    12. private:
    13. Ui::TransactionDialog *m_ui;
    14. ...more...
    To copy to clipboard, switch view to plain text mode 

    .cpp file
    Qt Code:
    1. TransactionDialog::~TransactionDialog()
    2. {
    3. qDebug("TransactionDialog::destructor");
    4. delete m_ui;
    5. if (clientDetailsDialogCreated)
    6. delete clientDetailsDialog;
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Destructor not being called

    it's not enough code. show us where do you create this object?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not being called

    why is your destructor virtual

  4. #4
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Destructor not being called

    I don't know why it's virtual, I must have used copy/paste from other existing code and didn't notice. Anyhow it made no difference.

    The object is created here:

    Qt Code:
    1. void MainWindow::openRecord(int anID, int aType)
    2. {
    3. if (aType < 4)
    4. {
    5. TransactionDialog *transactionDialog = new TransactionDialog(this, anID, aType);
    6. transactionDialog->show();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Destructor not being called

    is dtor of MainWindow called? looks like no.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not being called

    yeh, it wont make any difference, just that it being virtual here didnt make any sense..about your dialog, looking at the code, i m pretty sure that the destructor would be called when the MainWindow is deleted.

  7. #7
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Destructor not being called

    Quote Originally Posted by spirit View Post
    is dtor of MainWindow called? looks like no.
    Why would MainWindow's dtor have anything to do with it ?
    ... I see it now .....
    in the ctor, it needed:
    Qt Code:
    1. setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 

    Thanks for the kick !

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Destructor not being called

    Quote Originally Posted by vieraci View Post
    Why would MainWindow's dtor have anything to do with it ?
    ... I see it now .....
    in the ctor, it needed:
    Qt Code:
    1. setAttribute(Qt::WA_DeleteOnClose);
    To copy to clipboard, switch view to plain text mode 

    Thanks for the kick !
    because, if you remeber, when you pass a parent into ctor of objects which subclassed from QObject Qt will delete all children by itself when a parent's dtor is being called. but if you don't pass a parent in ctor of objects then you have to delete these objectcs by yourself.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. The following user says thank you to spirit for this useful post:

    vieraci (15th May 2009)

  10. #9
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Destructor not being called

    Quote Originally Posted by talk2amulya View Post
    just that it being virtual here didnt make any sense..
    Why? The dtor is virtual in base class already. With or without virtual makes no difference in his class. But adding virtual will remind people the method exists in vtable...

    I add "virtual" keyword in derived class if the method is already defined as virtual in the based class...as I said, it is not necessary but it is a good reminder...

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Destructor not being called

    Quote Originally Posted by lni View Post
    Why?
    I guess the intention of the author was that no other methods are virtual so the destructor doesn't need to be virtual as well. Regardless of what happens higher in the inheritance tree.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not being called

    Another way to call the dtor is to create the object NOT by a pointer-var like in your case:

    Qt Code:
    1. ...
    2. TransactionDialog *transactionDialog = new TransactionDialog(this, anID, aType);
    3. transactionDialog->show();
    4. ...
    To copy to clipboard, switch view to plain text mode 

    but as an object directly, like:

    Qt Code:
    1. ...
    2. TransactionDialog transactionDialog(this, anID, aType);
    3. transactionDialog.show();
    4. ...
    To copy to clipboard, switch view to plain text mode 

    That is only if you are not restricted in the way you have to create your object.

    I had the same problem on a case with my mainwindow-object in a main.cpp file.
    (problem was also solved after i added line 9 for the pointer version)
    Here are the two ways that work:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4.  
    5. // FIRST way to reach dtors
    6. CPlotterMainWindow *plotmainwindow = NULL;
    7. plotmainwindow = new CPlotterMainWindow();
    8. plotmainwindow->setAttribute(Qt::WA_QuitOnClose);
    9. plotmainwindow->setAttribute(Qt::WA_DeleteOnClose); // needed to reach dtors
    10. plotmainwindow->show();
    11.  
    12. // SECOND way to reach dtors
    13. // CPlotterMainWindow plotmainwindow;
    14. // plotmainwindow.setAttribute(Qt::WA_QuitOnClose);
    15. // plotmainwindow.show();
    16.  
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    Greetings

  13. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Destructor not being called

    if you dialog is meant to be scope limited, then there is no point in using new and creating the dialog on the heap. The only reason to use the heap is for longer lifetime of instances (or occasionally forced by memory limit of stack).

    Therefore you should not be using 'new' on a dialog if you are also going to use WA_DeleteOnClose (since it will necessarily be closed before it exits scope).
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  14. #13
    Join Date
    Jun 2008
    Posts
    17
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not being called

    Quote Originally Posted by amleto View Post
    if you dialog is meant to be scope limited, then there is no point in using new and creating the dialog on the heap. The only reason to use the heap is for longer lifetime of instances (or occasionally forced by memory limit of stack).

    Therefore you should not be using 'new' on a dialog if you are also going to use WA_DeleteOnClose (since it will necessarily be closed before it exits scope).
    Hi amleto!

    The "new" way is often used in many examples of qt and unless you use a modified destructor with some output for example, you will not notice that it will not be called.
    You can try that in my example mentioned before. Just insert a qDebug() output in the "CPlotterMainWindow()" (or your own class that you call with "new") and you will see
    that it will not be reached if that "WA_DeleteOnClose" is not there (but i think you already know that).
    It was just an example there. But it still shows what it's about to show.
    I didn't want to start a programming-style-discussion here
    I just tried to show the way(s) how you can "reach" the destructor. And I think that this example does that.

    I probably should have written that for your case:
    Qt Code:
    1. void MainWindow::openRecord(int anID, int aType)
    2. {
    3. if (aType < 4)
    4. {
    5. TransactionDialog *transactionDialog = new TransactionDialog(this, anID, aType);
    6. transactionDialog->show();
    7. transactionDialog->setAttribute(Qt::WA_QuitOnClose);
    8. transactionDialog->setAttribute(Qt::WA_DeleteOnClose);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Cheers

Similar Threads

  1. setSceneRect not being called properly?
    By bjh in forum Qt Programming
    Replies: 7
    Last Post: 12th July 2009, 19:42
  2. Replies: 4
    Last Post: 19th April 2009, 15:51
  3. filterAcceptsRow is not called.
    By kaushal_gaurav in forum Qt Programming
    Replies: 1
    Last Post: 10th February 2009, 08:05
  4. Replies: 1
    Last Post: 7th August 2008, 13:46
  5. QWidget::resizeEvent is not called
    By maximAL in forum Qt Programming
    Replies: 9
    Last Post: 3rd February 2008, 16:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.