QInputDialog::getItem gives segmentation fault

Report any problems with CopperSpice
Post Reply
fingal_ohlson
Posts: 3
Joined: Wed Nov 08 2023 2:50 pm

QInputDialog::getItem gives segmentation fault

Post by fingal_ohlson »

I compiled the Copperspice library on Macos 13.6.1 (Ventura), and I am going through the examples in the Copperspice journal (https://journal.copperspice.com/?page_id=932). When I got to example 12, I encountered an error.

Here's what happens:
  1. I start example_12 from in a terminal window.
  2. The main window appears with two push buttons and two edit fields.
  3. I press the push button marked "Item" and a dialog box titled "Item" appears containing a combo box with the items "Spring", "Summer", "Fall" and Winter"
  4. I pick an item from the combo box or I just ignore it.
  5. I leave the dialog by pressing "OK", "Cancel" or the red button with the cross in the left corner.
  6. The dialog box disappears together with the main window.
  7. The terminal shows the message "Segmentation fault: 11".
The error seems to be in this piece of code:

void MainWindow::setItem()
{
...QStringList itemList;
...itemList << "Spring" << "Summer" << "Fall" << "Winter";

...bool ok;

...QString data = QInputDialog::getItem(this, "Item",
........"Select a Season:", itemList, 0, false, &ok);

...if (ok && ! data.isEmpty()) {
......item_text->setText(data);
...}
}


The expected behaviour is that the dialog box closes and that the selected text (alternatively the default selection if I don't pick anything) is shown in the edit field to the right of the push button.

I looked up the source for QInputDialog::getItem and that prompted me to try the following:

void MainWindow::setItem()
{
...QStringList itemList {"Spring", "Summer", "Fall", "Winter"};

...bool ok;

...QInputDialog *dialog = new QInputDialog(this);

...dialog->setComboBoxEditable(false);
...dialog->setComboBoxItems (itemList);
...dialog->setLabelText("Select a Season:");
...dialog->setWindowTitle("Item");

...const int ret = dialog->exec();

...if (ret == QInputDialog::Accepted) {
......QString data = dialog->textValue();
......item_text->setText(data);
...}

...// NB! Notice the following line is commented out. If not the program crashes.
...// delete dialog;
}


This code gives the expected behaviour. It only works, however, if I don´t release the QInputDialog object with "delete dialog". So it seems something happens when the object is deleted - either because it (the QInputDialog object in the static ::getItem method) goes out of scope like in the original example_12, or because it is released with delete like I did in my code.
barbara
Posts: 454
Joined: Sat Apr 04 2015 2:32 am
Contact:

Re: QInputDialog::getItem gives segmentation fault

Post by barbara »

Thanks for submitting such a detailed report. It turns out a CopperSpice team member discovered this a few weeks ago. It only occurs on platforms using libc++ which affected us on Mac OS X and FreeBSD.

The correction made it through CI and was pushed to github. If you build CopperSpice from the latest commit (SHA 775403C) Journal Example 12 will build and run.

Please let us know if you find any other problems.

Barbara
fingal_ohlson
Posts: 3
Joined: Wed Nov 08 2023 2:50 pm

Re: QInputDialog::getItem gives segmentation fault

Post by fingal_ohlson »

Thanks for the help. That solved the problem.

I was curious to see whether the latest commit also would make the combo boxes respond quicker. It did not. On Macos there's a delay of more than a second from you click on an item to it is shown as selcted. You even have time to move the mouse pointer to another item, and that item will be selected instead of the one you clicked on. This seems to happen everywhere I encounter a combo box - whether it's in precompiled binaries or programs I have compiled myself.
Post Reply