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.dang...@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

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

_______________________________________________
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest

Reply via email to