Program crashes when calling QComboBox::clear() on a non-empty Combo Box



  • Hi all,

    I'm currently working on a project in Qt and I have come across a problem in my project that if I try to call clear on an empty Combo Box there is no problem but when I call clear on a ComboBox with items in it then the program crashes. All it tells me is The program has unexpectedly finished.

    This isn't just for one form, it is for all forms that I am using but I'll post the details of the one that alerted me to the problem.

    #ifndef CITYUI_H
    #define CITYUI_H
    
    #include <QMainWindow>
    
    #include <string>
    
    #include "city.h"
    
    namespace Ui {
    class CityUI;
    }
    
    class CityUI : public QMainWindow {
        Q_OBJECT
    public:
        explicit CityUI(QWidget *parent = 0);
        ~CityUI();
    
        const std::string& getCityName() const;
        void setCityName(const std::string& city);
    
    public slots:
        void updateDisplay();
    
    private:
        Ui::CityUI *ui;
        std::string city;
    
        std::vector<const std::string> alreadyAdded;
    
        bool alreadyAddedItem(const std::string& person);
    
    private slots:
        void shopButtonClicked();
        void talkButtonClicked();
        void shopComboIndexChanged();
        void personComboIndexChanged();
    
    signals:
        void playerChanged();
    };
    
    #endif // CITYUI_H
    

    For brevity I am only including the updateDisplay()

    void CityUI::updateDisplay() {
        City city = allCities[this->city];
    
        PeopleVector people = city.getPeople();
        ShopVector shops = city.getShops();
    
        //this->ui->personComboBox->clear();
        for (const auto& aPerson : people) {
            if (!this->alreadyAddedItem(aPerson)) {
                Person person = allPeople[aPerson];
                this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));
                this->alreadyAdded.push_back(aPerson);
            }
        }
    
        //this->ui->shopComboBox->clear();
        for (const auto& aShop : shops) {
            Shop shop = allShops[aShop];
            this->ui->shopComboBox->addItem(QString::fromStdString(shop.getName()));
        }
    
        std::string personComboEntry = this->ui->personComboBox->currentText().toStdString();
        std::string shopComboEntry   = this->ui->shopComboBox->currentText().toStdString();
    
        std::string stringToBeWritten = "Shop: ";
        stringToBeWritten.append(allShops[shopComboEntry].getDesc());
        stringToBeWritten.append("\nPerson: ");
        stringToBeWritten.append(allPeople[personComboEntry].getDesc());
    
        this->ui->gameOutput->setText(QString::fromStdString(stringToBeWritten));
    
        this->ui->personComboBox->setDuplicatesEnabled(false);
        this->ui->shopComboBox->setDuplicatesEnabled(false);
    }
    

    I'm not entirely sure why this is happening but any help that can be given would be greatly appreciated.


  • Moderators

    @PurityLake

    Hi and welcome to the forum

    My guess is that it is not the clear, but an access to a non-existing item afterwards.

    Can you run it in the debugger? There you can find at which line the actual crash is occuring.

        std::string personComboEntry = this->ui->personComboBox->currentText().toStdString();
        std::string shopComboEntry   = this->ui->shopComboBox->currentText().toStdString();
    
    

    Possibly there are no entries added for personComboBox after the clear. Therefore the retrieval of currentText will fail.


  • Moderators

    @PurityLake
    stupid question: are you calling setupUI() in the constructor to actually build the form widgets?



  • @koahnig

    Hi, the problem seems to be

    this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));
    

    Not the actual clear() method.


  • Qt Champions 2016

    @PurityLake said:

    Person person = allPeople[aPerson];

    Hi
    does this return a valid person ?
    only thing i can think of why it should crash in
    this->ui->personComboBox->addItem(QString::fromStdString(person.getName()));

    if you change to
    this->ui->personComboBox->addItem("TEST");

    does it still crash?

    also, as @raven-worx ask
    You have called
    setupUI()
    so that this->ui->personComboBox is in fact a valid object ?



  • @mrjj

    Ya it is a valid Person object.

    If you refer to my response to @koahnig you'll see my update


  • Qt Champions 2016

    @PurityLake
    ok.
    if you change to
    this->ui->personComboBox->addItem("TEST");

    does it still crash?



  • @mrjj

    Yes it still crashes


  • Qt Champions 2016

    @mrjj said:

    this->ui->personComboBox

    then pointer must be corrupt?


Log in to reply
 

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