Results 1 to 13 of 13

Thread: Destructor not being called

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    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.


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

    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

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

    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.

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

    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
  •  
Qt is a trademark of The Qt Company.