SurlyJake Blog

QT Segfault When Accessing Ui Objects

| Comments

I’m starting with QT for a hobby project. My design called for a public method inside of my QMainWindow main window. the method will update the text in the display. When calling anything using the ‘’ui->’‘ pointer, the app would segfault. The QT Creator debugger showed the ui pointer as ’‘. That was a clue. To confirm, I wanted to see gdb’s output.

To enable debug symbols for GDB, I had to edit the ‘.pro’ file and add the line ‘’#CONFIG += debug’‘. After the edit is made, change directories to the project folder, run qmake, make clean, and make. Once the build is completed, ‘gdb ./terminal’ (terminal is the name of my program).

$ gdb ./terminal
(gdb) run
Starting program: /home/jacob/program/./terminal 
Program received signal SIGSEGV, Segmentation fault.
0x0804f8f3 in MainWindow::updateStatus (this=0x2c74c085, status=0xbfffee4c) at mainwindow.cpp:233
233     qDebug() << ui->valueLabelLocusStatus->text();
(gdb) print ui
Cannot access memory at address 0x2c74c09d

After the segfault, I tried to get it to print the value of the ‘ui’ pointer, it said ‘’cannot access memorry at address…’‘. That confirmed it, Something is wrong with the ui pointer.

The problem was threading. Because my application was in a thread other than that of the main window, access to it’s widgets isn’t allowed. To resolve the issue, I followed the QT documentation at http://qt-project.org/doc/qt-5.0/qtcore/signalsandslots.html . I changed the public method on the mainwindow class into a QT slot. I made my class extend QObject and added a signal to my class instead of calling the method directly. Once the signal is connected to the slot, QT handled the threading issue.

Comments