[Interest] Crash when signal fires

Ben Haller bhaller at mac.com
Wed Apr 22 01:44:19 CEST 2020


Hi folks.  I’m seeing something weird that I don’t understand.  I have a connection defined for the main window of my Qt Widgets app, like so:

    connect(qApp, &QApplication::focusChanged, [this]() { updateUIEnabling(); });

I’m seeing a 100% reproducible crash as a result of this.  When the focusChanged signal fires, I end up in updateUIEnabling() as expected, but I immediately crash because something about the state of things is corrupted; the immediate symptom, according to the debugger, is that the ui pointer for my main window is equal to 0x1 (yuck).  I try to access a button,  ui->playOneStepButton, and it crashes.  As I said, 100% reproducible.

The weird thing is that this does *not* happen if I change the connect statement to this:

    connect(qApp, &QApplication::focusChanged, this, &QtSLiMWindow::updateUIEnabling);

Now, 100% of the time, there is no problem, no crash, and my app chugs along happily.  No other code changes involved.  I can literally flip this connect definition back and forth in my code and go from 100% crashing to 100% fine.

I’m pretty new to Qt and maybe I’m just being clueless somehow; but I thought that the two definitions above would be essentially identical in their effect.  Why does the first one crash?  Note that it works fine in general; it seems to crash specifically when I open a *second* main window (which makes its own connection to focusChanged in the same way), and then close that second main window.  Now the first main window’s connection is somehow unhappy, and the next time I trigger the focusChanged() signal, boom.  It’s as if the connection for the second window somehow interferes with the connection for the first window, and closing the second window leads to a corruption in the connection state.

Naturally, I am now nervous about using the first type of connection anywhere in my code, since I don’t understand what the issue is.  Can anyone shed light on this?

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University



More information about the Interest mailing list