PDA

View Full Version : Segmentation fault qComboBox



Cucus
11th July 2011, 21:50
Hi

When I try to clean my combo box to add new values (may not be the same number of elements) I have a segmentation fault. It always happens on the second time where the comboBox is modified. Here's the code:



void MenuPrincipal::updateComboBox(std::vector<std::vector<float> > solutions)
{
QString solution;
QList<QString> finalText;
solution = " ";

ui->comboBox->clear(); //segmentation fault

//adding objects...
for(int i=0;i<solutions.size();i++)
{
solution = "";
solution.append("(");
solution.append(QString::number(i+1));
solution.append(")");
solution.append(" -> ");
for(int j=0;j<6;j++)
{
solution.append(QString::number((double)solutions[i][j],'f',2));
if(j<solutions[i].size()-1)solution.append(" , ");
}
finalText.push_back(solution);
}

QStringList longerList = (finalText);
this->ui->comboBox->addItems(longerList);
}

stampede
11th July 2011, 22:54
Are you sure clear() causes crash ? Can you post backtrace ?
I see another problem in this code - you have to check if solutions[i] have enough elements before trying to access it, try this way:


const int count = solutions[i].count();
for(int j=0;j<6;j++){
if( j < count ){
solution.append(QString::number((double)solutions[i][j],'f',2));
if(j<count-1) solution.append(" , ");
}

Cucus
12th July 2011, 20:07
You can see here how I check where's the error:

First step
6655
Second step
6654

I control the number of elements of solution in another function.

stampede
14th July 2011, 20:56
Have you tried to run with debugger ? It will show you where it crashed exactly (you can clearly see if its inside QComboBox class).

robert.cerne
10th September 2011, 12:22
Hi,

I have had the same problem. Every time my application would modify the QComboBox object there was a high probability that a segmentation fault would occur. I have solved the problem by disabling the QComboBox. Example code is the following:



comboBoxComponent->setDisabled(true);
comboBoxComponent->clear();
comboBoxComponent->setDisabled(false);