PDA

View Full Version : Crash-Segmentation fault



poporacer
11th December 2010, 22:54
I have a stacked widget and I load classes into the stacked widget. On this form is a combo box (cmbName) that gets populated through an iteration of a database. Then there is a function call to set the currentIndex of the combobox to the index of the item with a specified id number (id from the database) I added another Qdialog and used the same code for setting the currentIndex and in this class the program crashes with a sementation fault. I searched this forum and other areas and searched my code. I could not find any of the things that were mentioned for a segmentation fault. Am I not seeing something? It looks like anytime I try to reference anything to do with the combobox I get the error. What could I be missing?

void SectionTimer::restoreName(int id)
{
if (id>=0)
{
qDebug()<<"id in section restore rider"<<id;
qDebug()<<"index in sectionTimer restore name"<<ui->cmbName->currentIndex();//this creates Segmentation fault
int index = ui->cmbName->findData(id);// if I comment out above line, this creates segmentation fault.
//qDebug()<<"index (calculated) in section restore name"<<index;
ui->cmbName->setCurrentIndex(index);
}
else
{
ui->cmbName->setCurrentIndex(0);
}
}
I use the exact same code elsewhere and there is no problem.
The id number is correct coming into the code. The index is not (there are only about 10 entries in the combobox)5592 The entry for the this pointer doesn't look correct?

SixDegrees
11th December 2010, 23:14
A segmentation fault is typically caused when you try to access memory that hasn't been allocated. A classic example is walking off the end of an array.

It's impossible to say what the problem is in this case; there's not enough code to make a determination. But I'd first look at whether your combo box is initialized properly, since attempting to access it's elements is what causes the problem.

The best thing to do would be to run the program in a debugger, which will let you quickly determine the cause of the problem.

poporacer
12th December 2010, 01:07
I used Designer to place the combobox. I checked the ui_h file and the combobox pointer is listed with the proper name cmbName. I have tried clean and rebuild. The combo box does get populated with this code:

void SectionTimer::updateNameComboBox()
{

QSqlQuery query;
query.exec("SELECT id,LName, FName FROM master");
ui->cmbName->blockSignals(true);
ui->cmbName->clear();

while (query.next())
{
ui->cmbName->addItem(query.value (2).toString()+ " "
+ query.value (1).toString(),query.value (0).toInt());
ui->cmbName->blockSignals(false);
}
}
And if I comment out the code listed that causes the segmentation fault, the program runs and the combobox is populated. I did run the debugger and posted the picture of the output. I tried to figure out how to get the back trace...is that what is shown in the lower left window?
What else do I need to look at?

SixDegrees
12th December 2010, 15:31
I can't read your screenshot very well. It looks like 'id' is 12 and 'index' is...some really large number. Are those the values you're expecting?

poporacer
12th December 2010, 23:13
No. I mentioned this in my first post as I thought this might be a problem. There are only around 10 entries in the combobox so the number is way off. Also the "this" pointer where the problem resides has a value of "unavailable sychroneous data". This doesn't look right to me either...but I am a newbie and I don't know what it should look like!
Thanks for your assistance!