Nominate our 2022 Qt Champions!

Q_ASSERT handler ?

  • Hi all,

    I wonder if there is a way to install own handler for Q_ASSERT in runtime ? Something similar to qInstallMessageHandler ?

  • Moderators

    No, there's no such thing. Asserts are there for debug purposes, if you're tripping an assertion, then you have a bug that you need to fix.

  • Yes indeed.

    Though, currently, I am running into such situation where I do have some tests which expects some failures under certain cases.
    In the code, however, there are some asserts which break the code flow. Due to that, testing is only possible in Release mode which usually is fine except the cases where there is something wrong and I want to debug it. Unfortunately, it is not easy as I need to comment out all asserts on the way and once bug is fixed, revert them back. A bit of a pain really :/

  • @enmaniac

    Are those Q_ASSERT in your own code?
    If the Q_ASSERT are in Qt, you need to fix the problems ASAP anyway, because you cannot rely on the outcome. Q_ASSERT is used when there is an unrecoverable situation.

    When you have the Q_ASSERT in your code and those are not essential, unrecoverable bugs, you might think over your strategy. Otherwise those bugs you have to fix anyway.

  • No it is not in QT code it is in a code which uses QT.
    Q_ASSERTS. in this case, are used to indicate contract violations. For instance, when client passes argument containing a wrong value into the function. The function can deal with it in such sense that it will not explode out of the suddent (to make sure in production code no crashes occur) but still in debug mode it is convinient to slap developers ego and show him thats something is really out of place.

    Eh indeed I need to think about it a bit more.

    Thanks for the input and for answering my question!

  • Moderators

    As said asserts are not intended to filter out user data, they are there to catch programming errors. If your user can pass you incorrect data, then it's the developer's job to sanitize it and if needed show error messages and handle the error. If the developer's not done his/hers job properly, then it should trip an assertion which is to show that the situation should be fixed. This is not a way to "slap developer's ego" but to catch their misses.

Log in to reply