[Interest] Advice on random program finish crash

Nuno Santos nunosantos at imaginando.pt
Wed Jan 7 13:36:52 CET 2015


Hi,

I’m not really sure. 

Question: 

- On program termination, isn’t all the memory allocated by it deleted?

With this approach at least the QNetworkAccessManager which is being allocated by the manager is supposedly being deleted:

Manager::~Manager()
{
    _networkAccessManager->deleteLater();
}

Which is called if I set app as Manager parent.

I’m not sure if this will result in a successful object deletion since the event loop is now terminated at this point.

While without any crash, i’m always looking to improve my understanding of this low level details. Any ideas, suggestions or explanations are welcome.

Thanks,

Regards,

Nuno

> On 07 Jan 2015, at 12:31, Michael Sué <sue at sf.mpg.de> wrote:
> 
> Hi,
>  
> With this approach, the object will no longer be deleted at program exit, right? So there can be no crash on deletion anymore.
>  
> - Michael.
>  
> From: interest-bounces+sue=sf.mpg.de at qt-project.org [mailto:interest-bounces+sue=sf.mpg.de at qt-project.org] On Behalf Of Nuno Santos
> Sent: Wednesday, January 07, 2015 1:16 PM
> To: Igor Mironchik
> Cc: Interests Qt
> Subject: Re: [Interest] Advice on random program finish crash
>  
> Hi,
>  
> I have made the following modification and it seems not crashing anymore:
>  
> static Manager* gManager = 0;
>  
> Manager* Manager::sharedManager()
> {
>           QMutexLocker lock(&managerMutex);
>  
>           if (!gManager)
>           {
>                       gManager = new Manager();
>           }
>  
>           return gManager;
> }
>  
> Does this seems a better approach?
>  
> Regards,
>  
> Nuno
>  
>> On 07 Jan 2015, at 10:30, Nuno Santos <nunosantos at imaginando.pt <mailto:nunosantos at imaginando.pt>> wrote:
>>  
>> Hi Igor,
>>  
>> Ops… You are right, it is not static.
>>  
>> I have tried your suggestion but now it crashes everytime I close the app and I can’t even get a stack trace like before. If I run it on debug, it points me to assembler code. Not anything I can really point to.
>>  
>> Regards,
>>  
>> Nuno
>>  
>>> 
>>> On 07 Jan 2015, at 10:19, Igor Mironchik <igor.mironchik at gmail.com <mailto:igor.mironchik at gmail.com>> wrote:
>>>  
>>> On Wed, 07 Jan 2015 13:01:32 +0300, Nuno Santos <nunosantos at imaginando.pt <mailto:nunosantos at imaginando.pt>> wrote:
>>> 
>>> 
>>> Yes,
>>> 
>>> int main(int argc, char **argv)
>>> {
>>>    QGuiApplication app(argc, argv);
>>> 
>>>    ...
>>> 
>>>    return app.exec();
>>> }
>>> 
>>> No. Here QGuiApplication is not static. Then in your app the situation is the next: QGuiApplication destroyes before your Manager. But any QObject-derived class must be created after QApplication and destroyed before QApplication. May be this is the reason of your crash.
>>> 
>>> Make QGuiApplication static. For example:
>>> 
>>> static QSharedPointer< QApplication > application( int argc = 0, char ** argv = 0 )
>>> {
>>>              static QSharedPointer< QApplication > app(
>>>                              new QApplication( argc, argv ) );
>>> 
>>>              return app;
>>> }
>>> 
>>> and in main()
>>> 
>>> QSharedPointer< QApplication > app = application( argc, argv );
>>> 
>>> before any QObject...
>>> 
>>> 
>>> 
>>> 
>>> 
>>> On 07 Jan 2015, at 09:54, Igor Mironchik <igor.mironchik at gmail.com <mailto:igor.mironchik at gmail.com>> wrote:
>>> 
>>> Hi, I have one question: is QApplication static too?
>>> 
>>> On Wed, 07 Jan 2015 12:32:21 +0300, Nuno Santos <nunosantos at imaginando.pt <mailto:nunosantos at imaginando.pt>>
>>> wrote:
>>> 
>>> 
>>> Hi,
>>> 
>>> I’m having a problem with a random crash that not always happens. I
>>> would like to have your advice on how to find the origin of it.
>>> 
>>> This crash always happens on program exit and the stack trace is the
>>> following:
>>> 
>>> Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
>>> 0   org.qt-project.QtCore                0x0000000109a03549
>>> QMutexPrivate::allocate() + 105
>>> 1   org.qt-project.QtCore                0x0000000109a02d1c
>>> QBasicMutex::lockInternal(int) + 92
>>> 2   org.qt-project.QtCore                0x0000000109a02c64 QMutex::lock() + 52
>>> 3   org.qt-project.QtCore                0x0000000109a0b6fd
>>> QWaitCondition::wait(QMutex*, unsigned long) + 173
>>> 4   org.qt-project.QtCore                0x0000000109a0aeae
>>> QThread::wait(unsigned long) + 110
>>> 5   org.qt-project.QtNetwork          0x000000010a4870da
>>> QNetworkAccessManagerPrivate::~QNetworkAccessManagerPrivate() + 58
>>> 6   org.qt-project.QtNetwork          0x000000010a48722e
>>> QNetworkAccessManagerPrivate::~QNetworkAccessManagerPrivate() + 14
>>> 7   org.qt-project.QtCore                0x0000000109c3e7c7
>>> QObject::~QObject() + 1879
>>> 8   org.qt-project.QtNetwork          0x000000010a483c16
>>> QNetworkAccessManager::~QNetworkAccessManager() + 262
>>> 
>>> The only particularity of this QNetworkAccessManager is the fact that it
>>> is instantiated on a class that is a singleton:
>>> 
>>> class Manager : public QObject
>>> {
>>> public:
>>> 
>>>              static Manager* sharedManager();
>>> 
>>>              ...
>>> 
>>>              QNetworkAccessManager* networkAccessManager();
>>> 
>>>              ...
>>> 
>>> protected:
>>> 
>>>              Manager();
>>>              ~Manager();
>>> 
>>>              ...
>>>              
>>>              QNetworkAccessManager _networkAccessManager;
>>> };
>>> 
>>> 
>>> Manager* Manager::sharedManager()
>>> {
>>>              QMutexLocker lock(&managerMutex);
>>>              static Manager manager;
>>>              return &manager;
>>> }
>>> 
>>> Manager::Manager() :
>>>              _networkAccessManager()
>>> {
>>>              ...             
>>> }
>>> 
>>> Manager::~Manager()
>>> {
>>>              
>>> }
>>> 
>>> Is there any obvious reason for this crash to happen randomly at program
>>> termination?
>>> 
>>> Thanks,
>>> 
>>> Regards,
>>> 
>>> Nuno Santos
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>>> http://lists.qt-project.org/mailman/listinfo/interest <http://lists.qt-project.org/mailman/listinfo/interest>
>>> 
>>> 
>>> --
>>> Best Regards,
>>> Igor Mironchik.
>>> _______________________________________________
>>> Interest mailing list
>>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>>> http://lists.qt-project.org/mailman/listinfo/interest <http://lists.qt-project.org/mailman/listinfo/interest>
>>>  
>>> 
>>> 
>>> -- 
>>> Best Regards,
>>> Igor Mironchik.
>> 
>>  
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>> http://lists.qt-project.org/mailman/listinfo/interest <http://lists.qt-project.org/mailman/listinfo/interest>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150107/7b6f0f12/attachment.html>


More information about the Interest mailing list