2

I derived an application class from QApplication in order to reimplement some methods. Here is the code:



    class MyApplication : public QApplication
    {
        Q_OBJECT
    private:
    public:
        //...
        virtual ~MyApplication();   
    };

    MyApplication::~MyApplication()
    {
        qDebug("~MyApp1");
        try
        {
            //some potentially long operations
        }
        catch(...)
        {
            qDebug("~MyApp  Exception");
        }

        qDebug("~MyApp2");
    }

    int main(int argc, char *argv[])
    {
        int returnValue = 1;
        {
            MyApplication app(argc, argv);
            returnValue = app.exec();
        }
        return returnValue;
    }

The problem is that I tend to get different qDebug outputs. I always get ~MyApp1 printed, but only sometimes ~MyApp2. What could be the reason? It seems, that when the App is closing, Qt does not let the whole destructor to be done. How can I make the program wait till the destructor is finished?

Fred
  • 4,894
  • 1
  • 31
  • 48
Effendi
  • 98
  • 6
  • 2
    Avoid throwing exceptions from the destructor. See also http://stackoverflow.com/questions/130117/throwing-exceptions-out-of-a-destructor – RA. Dec 10 '12 at 18:34
  • I know I should not throw exceptions in the destructor. try-catch statement is there just to prove there are no ecxeptions :) And there are no exceptions, because ~MyApp Exception is never printed out. – Effendi Dec 10 '12 at 18:38
  • Did you try stepping through it? – Stephen Chu Dec 10 '12 at 21:04
  • Another problem is that this bug never happens in the debug mode, only in the release. It looks like it has not enough time to clean everything up before the application is killed in the release. – Effendi Dec 11 '12 at 11:30

1 Answers1

2

The QApplication Destructor is called properly, it's just that qDebug probably shouldn't/can't be used at such a late state of the application. Try using cout/cin streams directly (iostreams/printf).

DerManu
  • 702
  • 4
  • 12
  • 1
    printf/cout would be indeed a good test. Still: ~MyApplication() is called before ~QApplication(), so there's still a valid QApplication. Also I don't see why the second qdebug in the dtor shouldn't work reliably if the first does. – Frank Osterfeld Dec 10 '12 at 19:49