App crashes clearing QComboBox



  • I have an app that queries a database for last names then populates a combobox. When the user selects a last name the first name combobox is supposed to clear and load first names that go with the selected last name. The first time through when the first name combo box is empty the clear has no effect. The second time through the app crashes. Both comboboxes are tied to slots so the clear firstname action in the lastname combobox callback is triggering the firstname slot while the lastname slot is still active. How can we clear the combo box without causing the crash. The code is shown below

    void GcfProjects::on_mLastNameList_currentIndexChanged(int index)
    {
    ui->mFirstNameList->clear();

    if (index >= 0)
    {
        QString name = ui->mLastNameList->itemText(index);
    
        QString errMsg;
        char tempBuf[255];
    
        QSqlQuery *searchQuery = new QSqlQuery(mSqlDatabase);
    
        std::string dbTableName = "gcf_2016.";
    
        dbTableName += mProjectToDbTableMap[mCurrentSpieces];
    
        QComboBox box;
    
        if (searchQuery != NULL)
        {
     //       if (dbTableName != NULL)
            {
                sprintf(tempBuf, "SELECT distinct first_name FROM %s "
                    "where last_name = '%s'", dbTableName.c_str(),
                    ui->mLastNameList->currentText().toStdString().c_str());
    
                searchQuery->prepare(tempBuf);
                if (!searchQuery->exec())
                {
                    QSqlError sqlError = searchQuery->lastError();
                    errMsg = sqlError.databaseText();
                    qDebug() << errMsg;
                }
                else
                {
                    if (searchQuery->isActive())
                    {
                        while (searchQuery->next())
                        {
                            QString name = searchQuery->record().value(0).toString();
    
                            ui->mFirstNameList->addItem(name);
                        }
    
                        searchQuery->finish();
                    }
                }
            }
    //		delete searchQuery;
        }
    }
    

    }

    void GcfProjects::on_mFirstNameList_currentIndexChanged(int index)
    {
    if (index >= 0)
    {
    QString name = ui->mLastNameList->itemText(index);
    }

    }


  • Qt Champions 2016

    @bclay
    Not exactly an answer to your question, but QComboBox box; is a local variable (and you don't seem to use it).
    Also this:

    char tempBuf[255];
    // ...
    sprintf(tempBuf, "SELECT distinct first_name FROM %s "
                    "where last_name = '%s'", dbTableName.c_str(),
                    ui->mLastNameList->currentText().toStdString().c_str());
    

    is just terrible!
    Use the QString's API, which is also type safe and doesn't require all those type conversions. E.g:

    QString query = QStringLiteral("SELECT distinct first_name FROM %1 where last_name = '%2'").arg(dbTableName).arg(ui->mLastNameList->currentText());
    

    Or better yet, bind your variables to the query (as described in the docs) so they will also be properly escaped.



  • Sorry to offend your senses. I have been coding for 35 years and mostly in C or C++. I have been coding Qt apps for only a year so the old school stuff comes out a lot.

    the "QComboBox box" is left over from some debugging and is not actually used at all. The app was created in QtCreator so the ui->mFirstNameList is specified in the auto-generated code.

    Still the question is how to get past the crash when I call clear on the combobox. I saw at one point something about disable but disabling the ui->mFirstNameList does not change anything and I do not see a way to get to the action to disable that. ui->mFirstNameList.action returns an empty list.


  • Qt Champions 2016

    @bclay

    Sorry to offend your senses.

    Not at all, I'm not that sensitive. :)

    Still the question is how to get past the crash when I call clear on the combobox.

    What do you get? Is it a failed assertion, segmentation fault? Would you provide a stack trace for the crash?

    Kind regards.



  • The error message is "GcfProjects.exe has triggered a breakpoint."

    The stack trace shows it to be way down to Qt5Cored.dll and into ntdll.dl. Nothing in the stack trace shows the app that failed. In other words it one of Visual Studio's useless errors.


  • Qt Champions 2016

    @bclay
    I don't see anything really suspicious in the code. However, I'm not a machine and will sometimes miss things, that's why I requested the stack. Anyway, perhaps someone else will find what I've missed.



  • I found what I was looking for but that did not fix the issue either. Basically I wrapped the calls to clear in blockSignal calls such as the following

    bool oldState = ui->mLastNameList->blockSignals(true);
    ui->mLastNameList->clear();
    ui->mLastNameList->blockSignals(oldState);
    

    Even though the signal was blocked the app still crashed then clear was called.

    Thanks for your suggestions.

    Bruce



  • @bclay
    One thing you could try is to manually add breakpoints and step through in debug mode to see how far your code goes and where it triggers the crash.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.