[Interest] Crash when signal fires

Giuseppe D'Angelo giuseppe.dangelo at kdab.com
Wed Apr 22 01:51:01 CEST 2020


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).

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

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

HTH,
-- 
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4329 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20200422/9c27931e/attachment.bin>


More information about the Interest mailing list