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. Why doesn't this work?
Forum Updated to NodeBB v4.3 + New Features

Why doesn't this work?

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 4 Posters 992 Views 1 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.
  • SPlattenS SPlatten

    I am debugging a project where I know some of the pointers used are nullptr, so I originally implemented calls to Q_ASSERT_X before the usage, however the execution didn't stop even though the pointer is nullptr, so I tried replacing with assert, same behaviour.

    assert(m_d!=nullptr);
    return m_d->smManager;
    

    With a breakpoint on the assert I can see that m_d is nullptr but it continues on as if nothing was wrong...

    [Edit]
    Qt Creator 4.12.4
    Qt 5.12.2
    GCC 8.5.0 20210514
    Operating System Red Hat 8.5.0-10 64 bit

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @SPlatten Debug or release build?
    If NDEBUG is defined assert will do nothing.

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    SPlattenS 2 Replies Last reply
    1
    • jsulmJ jsulm

      @SPlatten Debug or release build?
      If NDEBUG is defined assert will do nothing.

      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #3

      @jsulm , I will investigate, I am able to debug the project, is NDEBUG required in addition to this?

      Kind Regards,
      Sy

      1 Reply Last reply
      0
      • jsulmJ jsulm

        @SPlatten Debug or release build?
        If NDEBUG is defined assert will do nothing.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #4

        @jsulm I searched the entire project build and found this:

        # Disable asserts if building in release mode
        CONFIG(release, debug|release): DEFINES += NDEBUG
        

        Does this explain it ?

        Kind Regards,
        Sy

        jsulmJ 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @jsulm I searched the entire project build and found this:

          # Disable asserts if building in release mode
          CONFIG(release, debug|release): DEFINES += NDEBUG
          

          Does this explain it ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #5

          @SPlatten said in Why doesn't this work?:

          Does this explain it ?

          The question is: do you build in debug or release mode?
          If in release mode then NDEBUG will be set.
          You can also check compiler calls to see whether NDEBUG is set or not.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          SPlattenS 2 Replies Last reply
          0
          • jsulmJ jsulm

            @SPlatten said in Why doesn't this work?:

            Does this explain it ?

            The question is: do you build in debug or release mode?
            If in release mode then NDEBUG will be set.
            You can also check compiler calls to see whether NDEBUG is set or not.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #6

            @jsulm , I believe I'm building in debug mode as I can debug the C++ and the source is visible.

            Kind Regards,
            Sy

            jsulmJ 1 Reply Last reply
            0
            • jsulmJ jsulm

              @SPlatten said in Why doesn't this work?:

              Does this explain it ?

              The question is: do you build in debug or release mode?
              If in release mode then NDEBUG will be set.
              You can also check compiler calls to see whether NDEBUG is set or not.

              SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #7

              @jsulm I commented out that line, cleaned the project and rebuilt, the behaviour is still the same.

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • SPlattenS SPlatten

                @jsulm , I believe I'm building in debug mode as I can debug the C++ and the source is visible.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #8

                @SPlatten said in Why doesn't this work?:

                I believe I'm building in debug mode

                Why don't you simply check in which mode you're building?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                SPlattenS 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @SPlatten said in Why doesn't this work?:

                  I believe I'm building in debug mode

                  Why don't you simply check in which mode you're building?

                  SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #9

                  @jsulm I know I'm building debug because I can debug, I start the process and attach using Qt Creator, I wouldn't be able to do that with source if I was running a process built for release.

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by
                    #10

                    This line is actually not needed. But check if NDEBUG is used in your code explicitly.
                    CONFIG(release, debug|release): DEFINES += NDEBUG

                    if pro is used, you do not need to add anything for config. Maybe do not use mixed build "release | debug". Check the config setting in the .pro file.
                    if cmake is used, add
                    set( CMAKE_BUILD_TYPE Debug )

                    Q_ASSERT() works fine in my app.

                    SPlattenS 1 Reply Last reply
                    0
                    • JoeCFDJ JoeCFD

                      This line is actually not needed. But check if NDEBUG is used in your code explicitly.
                      CONFIG(release, debug|release): DEFINES += NDEBUG

                      if pro is used, you do not need to add anything for config. Maybe do not use mixed build "release | debug". Check the config setting in the .pro file.
                      if cmake is used, add
                      set( CMAKE_BUILD_TYPE Debug )

                      Q_ASSERT() works fine in my app.

                      SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #11

                      @JoeCFD , I commented out this line from the Makefile. It was the only occurrence found, but still Q_ASSERT_X has no effect.

                      Kind Regards,
                      Sy

                      JonBJ JoeCFDJ 2 Replies Last reply
                      0
                      • SPlattenS SPlatten

                        @JoeCFD , I commented out this line from the Makefile. It was the only occurrence found, but still Q_ASSERT_X has no effect.

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

                        @SPlatten
                        If sounds like you are compiling such that assert/Q_ASSERT/Q_ASSERT_X are no-ops.

                        Alternatively your assumption that the pointer is null is wrong. For example there are other reasons that m_d->smManager could crash than m_d being nullptr.

                        There is no point guessing about compiler flags when you can just look at the compile command being executed very easily.

                        Since you are changing code anyway, replace the assert(m_d!=nullptr); with something you know how it behaves:

                        if (m_d == nullptr)
                            QMessageBox() or write-to-file or whatever
                        
                        1 Reply Last reply
                        1
                        • SPlattenS SPlatten

                          @JoeCFD , I commented out this line from the Makefile. It was the only occurrence found, but still Q_ASSERT_X has no effect.

                          JoeCFDJ Offline
                          JoeCFDJ Offline
                          JoeCFD
                          wrote on last edited by JoeCFD
                          #13

                          @SPlatten can you copy your Q_ASSERT_X code here? As JonB wrote, use your logger code to show if it is a nullptr.
                          Also open your Makefile in build dir and -g has to be there. If not, make distclean and rebuild your project.
                          CFLAGS = -pipe -g -Wall -Wextra -D_REENTRANT -fPIC $(DEFINES)
                          CXXFLAGS = -pipe -g -std=gnu++1z -Wall -Wextra -D_REENTRANT -fPIC $(DEFINES)

                          if you see -g and -O2 or -O3, that is mixed build.

                          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