Results 1 to 3 of 3

Thread: Destructor not called

  1. #1
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Destructor not called

    Hi there,
    I have a program in my C++ application, so I developed a simple example to explain the problem:

    Main
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2.  
    3. #include "myinterface.h"
    4. #include "myconcreteclass.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9.  
    10. MyInterface *myConcreteObject = new MyConcreteClass();
    11. myConcreteObject->doSomething();
    12.  
    13. qDebug("Delete myConcreteObject");
    14. delete myConcreteObject;
    15.  
    16. return a.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 

    MyInterface
    Qt Code:
    1. #ifndef MYINTERFACE_H
    2. #define MYINTERFACE_H
    3.  
    4. class MyInterface
    5. {
    6. public:
    7. virtual void doSomething() = 0;
    8. };
    9.  
    10. #endif // MYINTERFACE_H
    To copy to clipboard, switch view to plain text mode 

    MyConcreteClass
    Qt Code:
    1. // Header
    2. #ifndef MYCONCRETECLASS_H
    3. #define MYCONCRETECLASS_H
    4.  
    5. #include "myinterface.h"
    6.  
    7. class MyConcreteClass : public MyInterface
    8. {
    9. public:
    10. MyConcreteClass();
    11. ~MyConcreteClass();
    12. void doSomething();
    13. };
    14.  
    15. #endif // MYCONCRETECLASS_H
    16.  
    17. // Body
    18. #include "myconcreteclass.h"
    19.  
    20. #include <QDebug>
    21.  
    22. MyConcreteClass::MyConcreteClass()
    23. {
    24. qDebug("Concrete class constructor");
    25. }
    26.  
    27. MyConcreteClass::~MyConcreteClass()
    28. {
    29. qDebug("Concrete class destructor");
    30. }
    31.  
    32. void MyConcreteClass::doSomething()
    33. {
    34. qDebug("Do something");
    35. }
    To copy to clipboard, switch view to plain text mode 

    Output
    Concrete class constructor
    Do something
    Delete myConcreteObject


    ... why the program didn't called the destructor of myConcreteClass?

    Sorry for my english

    Thanks, Dario

  2. #2
    Join Date
    Sep 2009
    Location
    Targu Mures, Romania
    Posts
    16
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Destructor not called

    Hello,

    Try to add a virtual destructor to your MyInterface class, like:

    Qt Code:
    1. class MyInterface
    2. {
    3. public:
    4. virtual void doSomething() = 0;
    5. virtual ~MyInterface(){}
    6. };
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Jancsi Farkas

  3. The following user says thank you to csaken for this useful post:

    satoshi (23rd April 2010)

  4. #3
    Join Date
    May 2009
    Posts
    29
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Destructor not called

    Perfect, it works fine

    Thanks! Dario

Similar Threads

  1. Destructor not being called
    By vieraci in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2012, 15:06
  2. destructor dilemma
    By Petr_Kropotkin in forum General Programming
    Replies: 3
    Last Post: 17th March 2010, 15:58
  3. QTemporaryFile and his destructor
    By SABROG in forum Qt Programming
    Replies: 3
    Last Post: 19th May 2009, 19:55
  4. Weird problems when destructor is called
    By cyberboy in forum Qt Programming
    Replies: 9
    Last Post: 21st October 2008, 13:59
  5. Destructor in QT
    By hgedek in forum Newbie
    Replies: 1
    Last Post: 18th September 2007, 10:54

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.