Results 1 to 6 of 6

Thread: Crash when qDebug in destructor

  1. #1
    Join Date
    Oct 2014
    Posts
    20
    Thanks
    2

    Default Crash when qDebug in destructor

    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 ()

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,391
    Thanks
    247
    Thanked 690 Times in 680 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Crash when qDebug in destructor

    And what happens when you comment out this line? Does the destructor execute without a crash?

  3. #3
    Join Date
    Oct 2014
    Posts
    20
    Thanks
    2

    Default Re: Crash when qDebug in destructor

    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::instanc e_)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;
    }
    Last edited by ugiwgh; 20th October 2014 at 01:17.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,391
    Thanks
    247
    Thanked 690 Times in 680 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Crash when qDebug in destructor

    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.

  5. The following 2 users say thank you to d_stranz for this useful post:

    sonulohani (21st October 2014), ugiwgh (21st October 2014)

  6. #5
    Join Date
    Oct 2014
    Posts
    20
    Thanks
    2

    Default Re: Crash when qDebug in destructor

    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?

  7. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crash when qDebug in destructor

    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.

  8. The following user says thank you to aamer4yu for this useful post:

    ugiwgh (24th October 2014)

Similar Threads

  1. Replies: 2
    Last Post: 9th September 2011, 09:57
  2. QSqlQuery crash in destructor
    By seim in forum Qt Programming
    Replies: 5
    Last Post: 22nd January 2010, 22:12
  3. QTableWidget Crash in Destructor
    By mclark in forum Qt Programming
    Replies: 19
    Last Post: 25th July 2008, 14:44
  4. Crash on QString Destructor
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2007, 14:28
  5. QList crash in destructor
    By mclark in forum Newbie
    Replies: 7
    Last Post: 6th December 2006, 15:27

Tags for this Thread

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.