Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. enable assertions in production code
Forum Updated to NodeBB v4.3 + New Features

enable assertions in production code

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 727 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Joachim W
    wrote on last edited by
    #1

    How to enable assertions in production code?

    Q_ASSERT is ignored if QT_NO_DEBUG is defined [1]. This is analogous to assert from <cassert>, which is ignored if NDEBUG is defined.

    However, this analogy is flawed because it is perfectly legitimate [2] to compile production code without NDEBUG, whereas, if I understand correctly, it is no good idea to configure Qt Release builds with undefined QT_NO_DEBUG.

    I want assertions to detect improbable bugs. If triggered, they shall terminate my application, and display an error message that contains source file and line number. Ideally through qFatal.

    Solutions:

    1. undefine QT_NO_DEBUG - which problems would that entail?
    2. write an ASSERT macro of my own - how would it get source file name and line number?
    3. use assert from <cassert> - again, how would it get source file name and line number, and how would it call qFatal?
    4. other ideas?

    [1] https://doc.qt.io/qt-5/qtglobal.html#Q_ASSERT
    [2] man 3 assert

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      Well, since you're doing something that is generally shunned in release code, yes, you would need to implement your own macros or functions to do this. getting source file and line number information is through compiler recognized macros that I don't remember off the top of my head.

      __source__ ???
      __line__ ???

      customers don't like error message and crash scenarios...it lowers their confidence in the product.

      JonBJ 1 Reply Last reply
      1
      • Kent-DorfmanK Kent-Dorfman

        Well, since you're doing something that is generally shunned in release code, yes, you would need to implement your own macros or functions to do this. getting source file and line number information is through compiler recognized macros that I don't remember off the top of my head.

        __source__ ???
        __line__ ???

        customers don't like error message and crash scenarios...it lowers their confidence in the product.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Kent-Dorfman said in enable assertions in production code:

        customers don't like error message and crash scenarios...it lowers their confidence in the product.

        Well, yes, but by the time the code is about to assert at runtime it's a bit late, isn't it...! You would advocate not outputting a message and just letting the code continue in whatever random state it's in, and perhaps crashing later anyway, first having wiped their data? :) Perhaps this is a conversation for elsewhere....

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved