I have a C++ program using Qt, with a GUI QObject that has a ComboBox (let's call it foo
) linked to a function (bar()
). This QObject is called object A
.
The combo box is linked in the constructor:
connect(foo, SIGNAL(currentIndexChanged(int)), SLOT(bar(int)));
In bar(int)
, this function is called at the end of the function:
inspector->update();
In the inspector's update
function, this happens:
A
is deleted (delete A;
, pretty much - it was created withnew
).A new instance of the same type as
A
is created, let's call it objectB
.
This is initialized and runs fine for a moment, then everything goes horribly wrong.
The program segfaults, but only due to a Qt error. GDB doesn't help much, but running the program through Valgrind memcheck produces a torrent of invalid reads such as this:
==23410== Invalid read of size 8
==23410== at 0xFE92D7A: QMetaObject::activate(QObject*, int, int, void**) (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.9.5)
==23410== by 0x79DDC94: QComboBox::currentIndexChanged(QString const&) (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.9.5)
==23410== by 0x79DFB2D: ??? (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.9.5)
...
==23410== Address 0x3218ca68 is 8 bytes inside a block of size 48 free'd
==23410== at 0x4C3123B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==23410== by 0xFE9119A: QObjectPrivate::deleteChildren() (in /usr/lib/x86_64-linux-gnu/libQt5Core.so.5.9.5)
==23410== by 0x7913D4B: QWidget::~QWidget() (in /usr/lib/x86_64-linux-gnu/libQt5Widgets.so.5.9.5)
This seems to me like the signal from foo
is being sent again, and is trying to call bar()
of object A
which doesn't exist. But how is this possible? Surely delete A
should also stop any signals from getting through? Also, I am only changing the value of the combo box once - why would multiple signals be sent?
I've tried blocking signals, and I've tried rearranging things, but with no success.
Here is the entire relevant Valgrind memcheck log.
Is this a bug with Qt? Or am I doing something wrong? Any help, including help with debugging further, is very much appreciated.