I am having some problems with a QObject that is being deleted when I don't expect it to.
I have this class (note that I don't declare Q_OBJECT)
class MyEntry : public QTreeWidgetItem
{
public:
MyEntry(QTreeWidgetItem* _parent, QString _name = "noname") :
QTreeWidgetItem(_parent, QStringList() << _name) {}
... it holds a few QStrings and has some setter/getter methods for them
};
I create and use this object in a class that contains a QTreeWidget. Every now and then this class "sends" one of its MyEntries to a different class which uses the info from the MyEntry it is being sent to set the text on some QLabels it holds. I use the following QEvent:
class MyEvent : public QEvent
{
public:
MyEvent(MyEntry* _ob) : QEvent((QEvent::Type)MYEVENT1), mp_ob(_ob) {}
~MyEvent() {}
MyEntry* getOb() { return mp_ob; }
private:
MyEntry* mp_ob;
};
I send and receive this event like so (the receiver is parent() and lp_currentEntry is a MyEntry object):
Class that contains the QTreeWidget:
QApplication::postEvent(parent() , new MyEvent(lp_currentEntry));
Class that contains the QLabels:
void MyOtherClass::customEvent(QEvent* _p_ev)
{
return;
}
The problem I have is that as soon as the event has been delivered, the MyEvent is being deleted. But the destructor of the MyEntry it contains is also called. I never created the MyEntry using the MyEvent as a parent so why is it being deleted?
I am using this setup because the postEvent is usually done by a thread which is not the ui one and I am pretty sure non-ui threads shouldn't update QLabels etc, otherwise I could have simply done (MyOtherClass*)parent()->updateLabels(lp_currentEntry);
- but correct me there if I am wrong.
Edit: Here is a backtrace:
#0 MyEntry::~MyEntry (this=0x7f83a0, __in_chrg=<optimized out>) at ../jb2/containers.cpp:171
#1 0x000000000040ede9 in MyEntry::~MyEntry (this=0x7f83a0, __in_chrg=<optimized out>) at ../jb2/containers.cpp:178
#2 0x0000000000416439 in ~MyEvent (this=0xfc7810, __in_chrg=<optimized out>) at ../jb2/jb2Events.h:28
#3 MyEvent::~MyEvent (this=0xfc7810, __in_chrg=<optimized out>) at ../jb2/jb2Events.h:28
#4 0x0000003ad317b45a in QCoreApplicationPrivate::sendPostedEvents(QObject*, int, QThreadData*) () from /lib64/libQtCore.so.4
#5 0x0000003ad31a5e33 in ?? () from /lib64/libQtCore.so.4
#6 0x0000003b24847825 in g_main_context_dispatch () from /lib64/libglib-2.0.so.0
#7 0x0000003b24847b58 in ?? () from /lib64/libglib-2.0.so.0
#8 0x0000003b24847c14 in g_main_context_iteration () from /lib64/libglib-2.0.so.0
#9 0x0000003ad31a5fc6 in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /lib64/libQtCore.so.4
#10 0x0000003ad4a6a5ee in ?? () from /lib64/libQtGui.so.4
#11 0x0000003ad31766ef in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>) () from /lib64/libQtCore.so.4
#12 0x0000003ad3176978 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>) () from /lib64/libQtCore.so.4
#13 0x0000003ad317b768 in QCoreApplication::exec() () from /lib64/libQtCore.so.4
#14 0x0000000000408f53 in main (argc=1, argv=<optimized out>) at ../jb2/main.cpp:10