Qt World Summit: Submit your Presentation

A problem about assert in QT

  • I'm using Creator. Qt verson is 5.15.2. The compiler is MinGW 8.1.0 32bit。
    The Operating system is windows 10.
    I have an exe and an dll.
    The exe invokes the dll and they runs well. t
    When I insert “assert(0&&"abc")” in the exe code, it will show the following message window. That's corret. The message tells me where the assertion failed and the Expression I wrote.

    When I insert "assert(0&&"abc")" in the dll code, it will show the following message
    This message is different from the first. It's useless.
    Actually, I run this via debug.
    The problem is that the same statement("assert(0&&"abc")") behaves differently in exe code and in dll code. Assert statement doesn't work in the dll code.
    This annoys me.
    Thank you.

    Hello, I did some more tests.
    When using msvc compiler, everything is right.
    click the exe code button
    click the dll code button.

    When using minGW compiler.
    click the exe code button.It is right.
    click the dll code button. In this example on my computer, the application crashed, showing nothing. Its window simply vanished.
    I don't know why. Maybe there is a bug in migGW compiler.
    Whatever, there is nothing I can do.
    Unfortunately, for some seasons, I have to use migGW to compile my project.

  • Lifetime Qt Champion

    @cq_qt said in A problem about assert in QT:

    Can anybody tell me what happened?

    You did something wrong and therefore Qt/c++ throws an assertion.

    Use a debugger, insect the stack trace and see where it crashes

  • @Christian-Ehrlicher
    My dll code and exe code are corrent and they runs well. the exe invokes the dll.
    When I insert “assert(0&&"abc")” in the exe code, it will show the first message window. That's corret.
    This message tells me where the assertion failed and the Expression I wrote.

    When I insert "assert(0&&"abc")" in the dll code, it will show the second message window because of "assert(0&&"abc")". The problem is this message is different from the first and useless.
    Why this happened? How can i solve it.

  • Lifetime Qt Champion

    I don't understand what your real problem is.. use a debugger!

  • @cq_qt
    Maybe the message is different when the assert() is in a DLL, I don't know. It would presumably be a PC thing anyway, or maybe compiler, but there's probably not much you can do about it if it behaves as you have shown.

    As @Christian-Ehrlicher says, why can't you run this via Debug from Creator (I admit I don't know if there is an QML issue about doing this)? Then when it hits that SEGV it should stop in the debugger and give you a stack trace, hopefully showing the DLL source code point.

  • Like the other commentators, I am also confused about why the "Signal Received - Qt Creator" popup window is being labeled entirely useless. If you click "OK" on that popup, doesn't the debugger stop at the line of the failed assertion? I would expect that it does. If not, then maybe instead of clicking "OK" you might try leaving that popup open and clicking the "pause/break" option in the debugger (unless this popup is modal and prevents you from doing that).

    Is the code in the DLL running in a (non-main, non-GUI) thread?

    In terms of why the outcome is different in a DLL, I can brainstorm several reasons.

    MinGW provides its own assert.h header and its own implementation of the assert feature. However, maybe when the DLL is compiled it "sees" some other copy of the assert.h (https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/assert-macro-assert-wassert)

    Also, the behavior of assertions is affected by things like #define NDEBUG being present in the source code.

    Ultimately the complete explanation is knowable and discoverable. But somebody would have to analyze the details of how you compile and link main.cpp, how you compile and link the DLL, what is on the loader path when you run it all in the debugger, and the full header-inclusion graph (to see if various headers that are "seen" during compilation are setting and/or unsetting preprocessor symbols in surprising ways).

    If you really want to know, then a great adventure awaits you :)

  • @cq_qt Your dll has release build and will not show assert message. Your app code has debug build which shows assert.
    if you want to see the message in both builds, use exception handling.

  • @KH-219Design
    thank you, I‘ll try some tests.

Log in to reply