[Interest] Crash when signal fires

Ben Haller bhaller at mac.com
Wed Apr 22 02:09:45 CEST 2020


> On Apr 21, 2020, at 7:51 PM, Giuseppe D'Angelo via Interest <interest at qt-project.org> wrote:
> 
> On 4/22/20 1:44 AM, Ben Haller via Interest wrote:
>> 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.
> 
> A quick checklist:
> 
> 0) I hope threads are not involved (you're not really allowed to do any GUI work from any other thread).

  No threads.

> 1) Never use the 3-arguments connect(). If you want to use the lambda, pass "this" as 3rd parameter and lambda as fourth. Why: if "this" has been destroyed, the first version won't disconnect automatically and crash. The second will disconnect and not crash.
> 
> More info: https://stackoverflow.com/a/27954502

  Ah, I see!  I was assuming it was my first window’s focusChanged connection that was crashing, but it was probably the second (closed) window’s connection that was crashing, in fact, because the window was gone but the connection remained.

> 2) ASAN, Valgrind and friends will help you locate the source of the crash if it's due to memory corruption. Use them.

  Unfortunately, I’m on macOS 10.15.3 and Valgrind is not there yet (I think at present they have “preliminary” support for macOS 10.13).

  I suspect this is a really dumb question, but what is “ASAN”?  Is that another Valgrind-like tool?

  Well, in any case, I think you have explained my bug.  Thanks very much!

Cheers,
-B.

Benjamin C. Haller
Messer Lab
Cornell University



More information about the Interest mailing list