PDA

View Full Version : Crash when qDebug in destructor



ugiwgh
19th October 2014, 11:05
I create a singleton class. I write "qDebug()<<"~Garbo";" in its destructor. But when close the program, its crash.
Any help will be appreciated.
The stack is following.
(gdb) bt
#0 0x0000003c82275795 in malloc_consolidate () from /lib64/libc.so.6
#1 0x0000003c82278612 in _int_malloc () from /lib64/libc.so.6
#2 0x0000003c82279a3d in malloc () from /lib64/libc.so.6
#3 0x0000003c8221fb21 in __gconv_open () from /lib64/libc.so.6
#4 0x0000003c8221f4c2 in iconv_open () from /lib64/libc.so.6
#5 0x00007f93fad70605 in QIconvCodec::createIconv_t (to=0x0, from=0x7f93fadd2d19 "UTF-16")
at /var/tmp/qt-x11-src-4.5.1/src/corelib/codecs/qiconvcodec.cpp:456
#6 0x00007f93fad70e3c in QIconvCodec::convertFromUnicode (this=<value optimized out>, uc=0x1e4da0a, len=3, convState=0x0)
at /var/tmp/qt-x11-src-4.5.1/src/corelib/codecs/qiconvcodec.cpp:328
#7 0x00007f93fad6b418 in QTextCodec::fromUnicode (this=0x71, str=<value optimized out>)
at /var/tmp/qt-x11-src-4.5.1/src/corelib/codecs/qtextcodec.cpp:1189
#8 0x00007f93fac93c31 in QString::toLocal8Bit (this=0x1c7ad50)
at /var/tmp/qt-x11-src-4.5.1/src/corelib/tools/qstring.cpp:3358
#9 0x000000000040c80e in QDebug::~QDebug (this=0x7fff65a4e7b0, __in_chrg=<value optimized out>)
at /opt/qtsdk-2009.02/qt/include/QtCore/qdebug.h:83
#10 0x000000000040ca53 in PaStyle::Garbo::~Garbo (this=0x619ca8, __in_chrg=<value optimized out>) at ./pa_style.h:23
#11 0x0000003c82235fd2 in exit () from /lib64/libc.so.6
#12 0x0000003c8221ec64 in __libc_start_main () from /lib64/libc.so.6
#13 0x0000000000406b09 in _start ()

d_stranz
19th October 2014, 18:32
And what happens when you comment out this line? Does the destructor execute without a crash?

ugiwgh
20th October 2014, 01:17
The destructor execute with a crash.

Added after 13 minutes:

The code is following.
std::cout runs ok, but qDebug runs failed. (qt 4.5.1)
-----------------------------------------------------------------------------------------------------
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QString>
#include <QScrollArea>
#include <QScrollBar>
#include <QDebug>
#include <QMutex>
#include <QList>

#include <iostream>
#include <cassert>

//---------------------- singleton class -----------------------
class PaStyle
{
public:
static PaStyle* getInstance();
class Garbo
{
public:
Garbo(){qDebug()<<"Garbo";}
~Garbo(){/*qDebug()<<"~Garbo";*/std::cout<<"~Garbo"<<std::endl;if(PaStyle::instance_)delete PaStyle::instance_;}
};
private:
PaStyle();
~PaStyle();
static PaStyle *instance_;
static QMutex lock_;
static Garbo garbo_;
};

PaStyle *PaStyle::instance_=NULL;
QMutex PaStyle::lock_;
PaStyle::Garbo PaStyle::garbo_;

PaStyle* PaStyle::getInstance()
{
if(NULL==instance_)
{
lock_.lock();
if(NULL==instance_)
{
qDebug()<<"New PaStyle Instance";
instance_=new PaStyle;
}
lock_.unlock();
}
assert(instance_);
return instance_;
}

PaStyle::PaStyle(){qDebug()<<"PaStyle";}
PaStyle::~PaStyle(){qDebug()<<"~PaStyle";}

//----------------- main.cpp ---------------------
int main(int argc,char **argv)
{
qDebug()<<"Main Begin";
QApplication app(argc,argv);

PaStyle *style=PaStyle::getInstance();
QWidget widget;
widget.show();

app.exec();
qDebug()<<"Main End";
return 0;
}

d_stranz
21st October 2014, 00:36
I don't see where you delete your singleton (PaStyle) instance, so apparently it is the C++ runtime cleanup that is deleting it. By this time, the QApplication instance is also gone, so anything that needs the QApplication instance to work (like the reference returned by qDebug()) is also gone. That's why you see the crash using qDebug() but not with std::cout.

If you add "delete style;" just before your "return 0;" statement in main(), you will see that you do not get a crash with qDebug().

Please use CODE tags when posting source code. Click "Go Advanced" when making a post, then click the "#" icon to insert the tags. Put your source in between the tags.

ugiwgh
21st October 2014, 05:49
Thanks for you help and the way of posting code.
The singleton deleted by system, because of static "PaStyle::Garbo PaStyle::garbo_;".
If I delete some class inherit from Qt in singleton destructor, it will crash also, is it?
It seems I must delete singleton before "return 0;" and after "app.exec();", am I right?

aamer4yu
21st October 2014, 08:54
A good practice is always to delete whatever you create.

So if you have created singleton, you should delete it too.
You need to have
delete style;

after the app.exec() code.