Qt-interest Archive, January 2007
Singleton with QDialog will coredump
Message 1 in thread
Hi Everyone,
I found a very strange coredump when I try to implement a
singleton (Design Pattern) QDialog.
I use QT designer to create a test.ui and uic convert it to test.h.
Its dialog object
Then I have my class :
class MyTestDialog : public QDialog, public Ui::testDialog
{
Q_OBJECT
public:
MyTestDialog& g_GetGlobalOne();
private:
MyTestDialog();
~MyTestDialog();
};
MyTestDialog& MyTestDialog::g_GetGlobalOne()
{
static MyTestDialog s_Dialog;
return s_Dialog;
}
MyTestDialog::MyTestDialog()
: QDialog(NULL)
{
...
}
MyTestDialog::~MyTestDialog()
{
...
}
When I try to exit my program, it will coredumps. The core stack located
inside
~MyTestDialog.
Please help me out.
Regards,
Pagan
I also append the content of test.h.
#ifndef TEST_H
#define TEST_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QTabWidget>
#include <QtGui/QTreeWidget>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
class Ui_testDialog
{
public:
QVBoxLayout *vboxLayout;
QTabWidget *tabWidget;
QWidget *tab;
QVBoxLayout *vboxLayout1;
QTreeWidget *treeWidget;
QWidget *tab_2;
void setupUi(QDialog *testDialog)
{
testDialog->setObjectName(QString::fromUtf8("testDialog"));
testDialog->resize(QSize(526,
549).expandedTo(testDialog->minimumSizeHint()));
vboxLayout = new QVBoxLayout(testDialog);
vboxLayout->setSpacing(6);
vboxLayout->setMargin(9);
vboxLayout->setObjectName(QString::fromUtf8("vboxLayout"));
tabWidget = new QTabWidget(testDialog);
tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
tab = new QWidget();
tab->setObjectName(QString::fromUtf8("tab"));
vboxLayout1 = new QVBoxLayout(tab);
vboxLayout1->setSpacing(6);
vboxLayout1->setMargin(9);
vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1"));
treeWidget = new QTreeWidget(tab);
treeWidget->setObjectName(QString::fromUtf8("treeWidget"));
treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
vboxLayout1->addWidget(treeWidget);
tabWidget->addTab(tab, QApplication::translate("testDialog", "Tab 1", 0,
QApplication::UnicodeUTF8));
tab_2 = new QWidget();
tab_2->setObjectName(QString::fromUtf8("tab_2"));
tabWidget->addTab(tab_2, QApplication::translate("testDialog", "Tab 2",
0, QApplication::UnicodeUTF8));
vboxLayout->addWidget(tabWidget);
retranslateUi(testDialog);
QMetaObject::connectSlotsByName(testDialog);
} // setupUi
void retranslateUi(QDialog *testDialog)
{
testDialog->setWindowTitle(QApplication::translate("testDialog",
"Dialog", 0, QApplication::UnicodeUTF8));
tabWidget->setTabText(tabWidget->indexOf(tab),
QApplication::translate("testDialog", "Tab 1", 0, QApplication:
:UnicodeUTF8));
tabWidget->setTabText(tabWidget->indexOf(tab_2),
QApplication::translate("testDialog", "Tab 2", 0, QApplicatio
n::UnicodeUTF8));
Q_UNUSED(testDialog);
} // retranslateUi
};
namespace Ui {
class testDialog: public Ui_testDialog {};
} // namespace Ui
#endif // TEST_H
When I
--
[ signature omitted ]
Message 2 in thread
Pagan Chou a écrit :
> Hi Everyone,
> I found a very strange coredump when I try to implement a
> singleton (Design Pattern) QDialog.
>
> I use QT designer to create a test.ui and uic convert it to test.h.
> Its dialog object
>
> Then I have my class :
>
> class MyTestDialog : public QDialog, public Ui::testDialog
> {
> Q_OBJECT
>
> public:
> MyTestDialog& g_GetGlobalOne();
>
> private:
> MyTestDialog();
> ~MyTestDialog();
> };
>
> MyTestDialog& MyTestDialog::g_GetGlobalOne()
> {
> static MyTestDialog s_Dialog;
> return s_Dialog;
> }
>
> MyTestDialog::MyTestDialog()
> : QDialog(NULL)
> {
> ...
> }
>
> MyTestDialog::~MyTestDialog()
> {
> ...
> }
>
> When I try to exit my program, it will coredumps. The core stack located
> inside
> ~MyTestDialog.
>
>
> Please help me out.
>
> Regards,
> Pagan
>
>
>
> I also append the content of test.h.
>
> #ifndef TEST_H
> #define TEST_H
>
> #include <QtCore/QVariant>
> #include <QtGui/QAction>
> #include <QtGui/QApplication>
> #include <QtGui/QButtonGroup>
> #include <QtGui/QDialog>
> #include <QtGui/QTabWidget>
> #include <QtGui/QTreeWidget>
> #include <QtGui/QVBoxLayout>
> #include <QtGui/QWidget>
>
> class Ui_testDialog
> {
> public:
> QVBoxLayout *vboxLayout;
> QTabWidget *tabWidget;
> QWidget *tab;
> QVBoxLayout *vboxLayout1;
> QTreeWidget *treeWidget;
> QWidget *tab_2;
>
> void setupUi(QDialog *testDialog)
> {
> testDialog->setObjectName(QString::fromUtf8("testDialog"));
> testDialog->resize(QSize(526,
> 549).expandedTo(testDialog->minimumSizeHint()));
> vboxLayout = new QVBoxLayout(testDialog);
> vboxLayout->setSpacing(6);
> vboxLayout->setMargin(9);
> vboxLayout->setObjectName(QString::fromUtf8("vboxLayout"));
> tabWidget = new QTabWidget(testDialog);
> tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
> tab = new QWidget();
> tab->setObjectName(QString::fromUtf8("tab"));
> vboxLayout1 = new QVBoxLayout(tab);
> vboxLayout1->setSpacing(6);
> vboxLayout1->setMargin(9);
> vboxLayout1->setObjectName(QString::fromUtf8("vboxLayout1"));
> treeWidget = new QTreeWidget(tab);
> treeWidget->setObjectName(QString::fromUtf8("treeWidget"));
> treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
>
> vboxLayout1->addWidget(treeWidget);
>
> tabWidget->addTab(tab, QApplication::translate("testDialog", "Tab 1", 0,
> QApplication::UnicodeUTF8));
> tab_2 = new QWidget();
> tab_2->setObjectName(QString::fromUtf8("tab_2"));
> tabWidget->addTab(tab_2, QApplication::translate("testDialog", "Tab 2",
> 0, QApplication::UnicodeUTF8));
>
> vboxLayout->addWidget(tabWidget);
>
> retranslateUi(testDialog);
>
> QMetaObject::connectSlotsByName(testDialog);
> } // setupUi
>
> void retranslateUi(QDialog *testDialog)
> {
> testDialog->setWindowTitle(QApplication::translate("testDialog",
> "Dialog", 0, QApplication::UnicodeUTF8));
> tabWidget->setTabText(tabWidget->indexOf(tab),
> QApplication::translate("testDialog", "Tab 1", 0, QApplication:
> :UnicodeUTF8));
> tabWidget->setTabText(tabWidget->indexOf(tab_2),
> QApplication::translate("testDialog", "Tab 2", 0, QApplicatio
> n::UnicodeUTF8));
> Q_UNUSED(testDialog);
> } // retranslateUi
>
> };
>
> namespace Ui {
> class testDialog: public Ui_testDialog {};
> } // namespace Ui
>
> #endif // TEST_H
>
>
> When I
>
>
Your destructor must not be private.
P@sNox,
--
[ signature omitted ]
Message 3 in thread
"Pagan Chou" <pagan_chou@xxxxxxxxxxxxxx> wrote in message
news:epcm4f$aej$1@xxxxxxxxxxxxxxxxxxxxx
> Hi Everyone,
> I found a very strange coredump when I try to implement a
> singleton (Design Pattern) QDialog.
>
> I use QT designer to create a test.ui and uic convert it to test.h.
> Its dialog object
>
> Then I have my class :
>
> class MyTestDialog : public QDialog, public Ui::testDialog
> {
> Q_OBJECT
>
> public:
> MyTestDialog& g_GetGlobalOne();
>
> private:
> MyTestDialog();
> ~MyTestDialog();
> };
>
> MyTestDialog& MyTestDialog::g_GetGlobalOne()
> {
> static MyTestDialog s_Dialog;
> return s_Dialog;
> }
>
> MyTestDialog::MyTestDialog()
> : QDialog(NULL)
> {
> ...
> }
>
> MyTestDialog::~MyTestDialog()
> {
> ...
> }
>
> When I try to exit my program, it will coredumps. The core stack located
> inside
> ~MyTestDialog.
>
>
> Please help me out.
The destructor will be called when the program shuts down, which is after
QApplication has been destroyed and disconnected from the X11 server.
You have to destroy (and therefore create) the dialog in a controlled
fashion, using operator new/delete. You can i.e. connect deleteLater to
QApplication::aboutToQuit.
Volker
--
[ signature omitted ]
Message 4 in thread
Volker Hilsheimer schrieb:
>> ...
>> MyTestDialog& MyTestDialog::g_GetGlobalOne()
>> {
>> static MyTestDialog s_Dialog;
>> return s_Dialog;
>> ...
> The destructor will be called when the program shuts down, which is after
> QApplication has been destroyed and disconnected from the X11 server.
>
> You have to destroy (and therefore create) the dialog in a controlled
> fashion, using operator new/delete. You can i.e. connect deleteLater to
> QApplication::aboutToQuit.
Usually the singleton pattern is implemented the following way:
// private static variable
MyTestDialog *MyTestDialog::m_instance = 0;
// public static method
MyTestDialog &MyTestDialog::getInstance()
{
if (m_instance == 0)
{
// create instance when needed
m_instance = new MyTestDialog();
}
return *m_instance();
}
// public static method
void MyTestDialog::destroyInstance() {
if (m_instance != 0)
{
delete m_instance;
m_instance = 0;
}
}
Make sure you call MyTestDialog::getInstance only AFTER a QApplication
instance has been created (which is usually not a problem).
Now before your application quits, destroy all singleton instances in
your program. For example the d'tor of the MainWindow might be a
suitable place:
MyMainWindow::~MyMainWindow()
{
..
// destroy all singelton instances
MyTestDialog::destroyInstance();
}
Or use the signal approach (deleteLater) as mentioned above.
Cheers, Oliver
--
[ signature omitted ]
Message 5 in thread
Hi Everyone,
Thanks for all your answering.
I've tried the deleteLater. But I still can't figure out how deleteLater
should be implemented. It is a SLOT. What should I connect to ??
Regards,
Pagan
"Volker Hilsheimer" <unwatched@xxxxxxx> ¦b¶l¥ó
news:epd0g3$5r0$1@xxxxxxxxxxxxxxxxxx ¤¤¼¶¼g...
>
> "Pagan Chou" <pagan_chou@xxxxxxxxxxxxxx> wrote in message
> news:epcm4f$aej$1@xxxxxxxxxxxxxxxxxxxxx
> > Hi Everyone,
> > I found a very strange coredump when I try to implement a
> > singleton (Design Pattern) QDialog.
> >
> > I use QT designer to create a test.ui and uic convert it to test.h.
> > Its dialog object
> >
> > Then I have my class :
> >
> > class MyTestDialog : public QDialog, public Ui::testDialog
> > {
> > Q_OBJECT
> >
> > public:
> > MyTestDialog& g_GetGlobalOne();
> >
> > private:
> > MyTestDialog();
> > ~MyTestDialog();
> > };
> >
> > MyTestDialog& MyTestDialog::g_GetGlobalOne()
> > {
> > static MyTestDialog s_Dialog;
> > return s_Dialog;
> > }
> >
> > MyTestDialog::MyTestDialog()
> > : QDialog(NULL)
> > {
> > ...
> > }
> >
> > MyTestDialog::~MyTestDialog()
> > {
> > ...
> > }
> >
> > When I try to exit my program, it will coredumps. The core stack located
> > inside
> > ~MyTestDialog.
> >
> >
> > Please help me out.
>
> The destructor will be called when the program shuts down, which is after
> QApplication has been destroyed and disconnected from the X11 server.
>
> You have to destroy (and therefore create) the dialog in a controlled
> fashion, using operator new/delete. You can i.e. connect deleteLater to
> QApplication::aboutToQuit.
>
> Volker
>
>
--
[ signature omitted ]
Message 6 in thread
Just call this->deleteLater(). There is no need to connect a signal to
it. Slots are just functions that have the added benefit of being able
to connect signals to them.
--Dave
Pagan Chou wrote:
> Hi Everyone,
> Thanks for all your answering.
>
> I've tried the deleteLater. But I still can't figure out how deleteLater
> should be implemented. It is a SLOT. What should I connect to ??
>
> Regards,
> Pagan
>
>
>
> "Volker Hilsheimer" <unwatched@xxxxxxx> ¦b¶l¥ó
> news:epd0g3$5r0$1@xxxxxxxxxxxxxxxxxx ¤¤¼¶¼g...
>
>> "Pagan Chou" <pagan_chou@xxxxxxxxxxxxxx> wrote in message
>> news:epcm4f$aej$1@xxxxxxxxxxxxxxxxxxxxx
>>
>>> Hi Everyone,
>>> I found a very strange coredump when I try to implement a
>>> singleton (Design Pattern) QDialog.
>>>
>>> I use QT designer to create a test.ui and uic convert it to test.h.
>>> Its dialog object
>>>
>>> Then I have my class :
>>>
>>> class MyTestDialog : public QDialog, public Ui::testDialog
>>> {
>>> Q_OBJECT
>>>
>>> public:
>>> MyTestDialog& g_GetGlobalOne();
>>>
>>> private:
>>> MyTestDialog();
>>> ~MyTestDialog();
>>> };
>>>
>>> MyTestDialog& MyTestDialog::g_GetGlobalOne()
>>> {
>>> static MyTestDialog s_Dialog;
>>> return s_Dialog;
>>> }
>>>
>>> MyTestDialog::MyTestDialog()
>>> : QDialog(NULL)
>>> {
>>> ...
>>> }
>>>
>>> MyTestDialog::~MyTestDialog()
>>> {
>>> ...
>>> }
>>>
>>> When I try to exit my program, it will coredumps. The core stack located
>>> inside
>>> ~MyTestDialog.
>>>
>>>
>>> Please help me out.
>>>
>> The destructor will be called when the program shuts down, which is after
>> QApplication has been destroyed and disconnected from the X11 server.
>>
>> You have to destroy (and therefore create) the dialog in a controlled
>> fashion, using operator new/delete. You can i.e. connect deleteLater to
>> QApplication::aboutToQuit.
>>
>> Volker
>>
>>
>>
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]