• Forum
  • Lounge
  • Visual Studio 2019 always failing an ass

 
Visual Studio 2019 always failing an assert

Let's look at some simple code:
1
2
3
4
5
6
#include <cassert>

int main()
{
   assert(5 > 6);
}


With VS2017 (and a few MinGW based compilers that I know of and test with) this assert fails only when compiled in debug mode. Release mode and no assert failure.

Compile with VS2019 and no matter if compiled in Debug or Release mode and the assert fails.

In 2019 debug mode the Windows debug dialog pops up, the release fails "silently." No dialog box.

Seems like a rather serious bug to me. What you all think?
In 2019 debug mode the Windows debug dialog pops up, the release fails "silently." No dialog box.
What? If the assertion fails silently how can you tell it's failing? The program does nothing else.

An assertion being tested in release mode doesn't seem like such a big deal to me. Sure, it's a bug, but if your assertion failed the program probably had a bug anyway. I have at times written my own asserts that are always tested and throw catchable std::exceptions.
There is still console output, that is how.

Release mode console output for 2019:
Assertion failed: 5 > 6, file D:\Programming\Projects 2019\cplusplus2019\Project6\Source6.cpp, line 5


Same console output when run in debug mode.

2017 release there is no console output, it is blank.
I modified the source a bit:
1
2
3
4
5
6
7
8
9
#include <cassert>
#include <iostream>

int main()
{
   assert(5 > 6); // always fails (when compiled in debug mode)

   std::cout << "Was there an error?\n";
}

Run from within the VS IDE:

VS2017 debug console output (with a MSC++ runtime library debug error dialog, select abort):
Assertion failed: 5 > 6, file d:\programming\projects 2017\cplusplus2017\project6\source6.cpp, line 6

D:\Programming\Projects 2017\cplusplus2017\Debug\Project6.exe (process 2928) exited with code 3.

VS2017 release console output:
Was there an error?

D:\Programming\Projects 2017\cplusplus2017\Release\Project6.exe (process 5788) exited with code 0.

VS2019 debug console output (with error dialog, select abort):
Assertion failed: 5 > 6, file D:\Programming\Projects 2019\cplusplus2019\Project6\Source6.cpp, line 6

D:\Programming\Projects 2019\cplusplus2019\Debug\Project6.exe (process 14724) exited with code 3.

VS2019 release console output:
Assertion failed: 5 > 6, file D:\Programming\Projects 2019\cplusplus2019\Project6\Source6.cpp, line 6

D:\Programming\Projects 2019\cplusplus2019\Release\Project6.exe (process 2332) exited with code -1073740791.


The exit code with the 2019 release run is.....a bit, unusual IMO.
Last edited on
Running it from the IDE is not the same as running it normally. Compiling Debug vs Release has some effect on how the IDE behaves, but the IDE hooks the program either way.


C:\Users\Michael\Programming\cpp\cpp.com\foo
$ cl /EHsc /Ox /std:c++17 a.cpp /DNDEBUG=1
Microsoft (R) C/C++ Optimizing Compiler Version 19.20.27508.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

a.cpp
Microsoft (R) Incremental Linker Version 14.20.27508.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:a.exe
a.obj

C:\Users\Michael\Programming\cpp\cpp.com\foo
$ a
Was there an error?

[edit] Running without NDEBUG defined produces the correct assertion failure.

Hope this helps.

[edit] Console is ConEmu
Last edited on
Running it from the IDE is not the same as running it normally.

I also ran the various exe files from a command prompt and got the same results.

It looks like 2019 is not defining NDEBUG when using the IDE to compile release mode, 2017 apparently is defining the macro. As it should.

That I would call a definite bug.
Umm, I have never known NDEBUG to be automatically defined.

But I tend to do everything from the command line anyway...
But I tend to do everything from the command line anyway...

And I do most of the work via the IDE, not being the expert you are.

I can't say VS2017 actually defines NDEBUG with release mode; I can say assert() works as if it were.

Just as I can say VS2019 assert() doesn't work as it is supposed to, according to the standard.

This is just a heads up for anyone using VS2019 who compiles via the IDE. They could be bit by the assert() bug.
No, it works just like it is supposed to; I just proved it to you with my own VS 2019.

I think you are expecting behavior from the IDE that has nothing to do with the C++ standard.


Also, I make no pretense of any expertise with VS. I stick to the terminal precisely to avoid spending time on plumbing VS's depths.
[Edit] But mostly because I dont like the code editor
Last edited on
it works just like it is supposed to

I emphatically disagree, period.

The IDE with 2019 doesn't apply NDEBUG when it is release mode, as it did with 2017. I checked the command line options in both and noticed the command line switch in 2019 was missing.

A command line switch you utilize when you compile via the command line.

I added a custom command switch to my project's settings for release mode (/D "NDEBUG") and now the assert doesn't fail. As it should per the standard.

[quote]I stick to the terminal precisely to avoid spending time on plumbing VS's depths./quote]
To each his own. I do use the IDE. And when something doesn't work as expected I mention it.
Hey, FG, I understand you are frustrated by the change, but you are making assertations contrary to fact. It is not part of any standard to predefine NDEBUG for any circumstance — it is totally up to the implementation how to handle that.

What is standard is that you can #define and #undef NDEBUG as many times as you want in any translation unit.

If it is that big of an issue (it would annoy me too), change VS’s global C++ project options and then you will never have to be annoyed by it again. Open a project (any project), then enable the Property Manager under the View menu.

In the Build Types in the Property Manager there are a number of different options (like “Release | x64”). The first item under each one is the global properties sheet (like “Microsoft.Cpp.x64.user”). Right-click and choose Properties and you will get the nice dialog to modify the global project options for a x64 C++ Release build.

Add the NDEBUG to your list of defines and save.

(You could also edit the XML directly, but it is easier from the IDE.)

Enjoy!
Curious, I decided to try a quick console compilation, VS 2019

Here, the release build did not fire the assert, while the debug version did.

So, it CAN work as you expect, I'm just not sure why it isn't on your machine.
Last edited on
Topic archived. No new replies allowed.