Why doesn't this work?
-
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 -
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 -
@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 ?
@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. -
@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. -
@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. -
@jsulm , I believe I'm building in debug mode as I can debug the C++ and the source is visible.
@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?
-
@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?
-
This line is actually not needed. But check if NDEBUG is used in your code explicitly.
CONFIG(release, debug|release): DEFINES += NDEBUGif 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.
-
This line is actually not needed. But check if NDEBUG is used in your code explicitly.
CONFIG(release, debug|release): DEFINES += NDEBUGif 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.
-
@JoeCFD , I commented out this line from the Makefile. It was the only occurrence found, but still Q_ASSERT_X has no effect.
@SPlatten
If sounds like you are compiling such thatassert
/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 thanm_d
beingnullptr
.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
-
@JoeCFD , I commented out this line from the Makefile. It was the only occurrence found, but still Q_ASSERT_X has no effect.
@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.