PDA

View Full Version : Clearing a QComboBox causes a crash



tony@hitachi
8th March 2007, 12:26
I have a problem clearing the contents of a QComboBox.

The program I have written has two QComboBox objects in a QMainWindow derived window. Both are populated with QStringlists and, initially, work a treat. However, when I need to re-populate one of them, the program crashes when I call the combo box's clear() method. This happens if I call the method directly or through a signal. It only happens with one of the boxes, not the other. Also, if I have only one box in the window ( the other being commented out) there is no problem.

Stepping through the code I have found that line number 2910 in QObject.cpp is throwing an exception : -

try {
c->receiver->qt_metacall(QMetaObject::InvokeMetaMethod, method, argv ? argv : empty_argv);
} catch (...) {
if (c->receiver) {
c->receiver->d_func()->currentSender = previousSender;
c->receiver->d_func()->currentSenderSignalIdStart = previousFrom;
c->receiver->d_func()->currentSenderSignalIdEnd = previousTo;
}
throw;

Here "previousSender" is 0, both "previousFrom" and "previousTo" are -1.

Any ideas or suggestions will be greatfully received. If you need any further information please feel free to contact me.

I am using Qt version 4.2.1. The development environment is MS Visual Studio 2003 .NET with Visual C++ .NET with the Qt Visual Studio Integration version 1.1.3.

AdvThanksance.

Tony

wysota
8th March 2007, 12:45
Could you provide a minimal compilable example reproducing the problem?

tony@hitachi
8th March 2007, 15:36
I have pared down the code to a minimum so it just includes opening an xml data file, reading in the data and populating the two combo boxes. When the "Go" button is pushed the combo boxes are re-populated with the same data. The crash occurs in the populateView() method of the MissingWords class at the line where the "subject" combo box is cleared.

See attached zip file for source code and "pro" file.

Cheers,

T@H

jpn
8th March 2007, 16:04
Well, that's far from minimal, but... :)

For me it crashed in MissingWords::populateSceneComboBox(), which got called from MissingWords::on_subject_comboBox_currentIndexChan ged() with a null item.

wysota
8th March 2007, 16:04
It is "MissingWords::populateSceneComboBox" that crashes and not clear().
"subjectitem" argument there is null. Take a look:

DomItem* subjectItem = static_cast< DomItem*>( m_CurrentSubjectIndex.internalPointer());
populateSceneComboBox( subjectItem);
And debugging gives:

#2 0x0804cba8 in DomItem::child (this=0x0, i=0) at domitem.cpp:75
#3 0x0804f00c in MissingWords::populateSceneComboBox (this=0xbfb556bc, subjectItem=0x0) at missingwords.cpp:112
#4 0x0804f0fc in MissingWords::on_subject_comboBox_currentIndexChan ged ( this=0xbfb556bc, index=-1) at missingwords.cpp:152

Take a look at #3 - "subjectItem=0x0" - a null pointer. This propagates to #2 - "this" is a null pointer as well, therefore you're trying to dereference a null pointer which causes the segmentation fault (or memory access violation in your case).

tony@hitachi
8th March 2007, 16:20
It looks like the xml file is not being opened successfully, hence the subjectItem pointer is a NULL. When I run the program the xml file is opened and subjectItem is not a NULL pointer.

Thanks for the input so far.

Cheers,

T@H

P.S. Fair do's, I should have checked in populateSceneComboBox() for a NULL pointer!

tony@hitachi
8th March 2007, 16:37
I have the solution.

When the subject combo box is cleared it generates an "on_subject_comboBox_currentIndexChanged()" signal with an index value of -1. The code does not check the index which means subjectItem is a NULL value which, as you have already pointed out, is what is causing the crash.

Many thanks for your help.

T@H