473,941 Members | 4,217 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

release build assertions/verifications

I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:

#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
#else
#include <cassert>
#endif

Unfortunately, NDEBUG could be defined to something meaningful, in
which case it's value would be lost.

Can anyone think of some other way to use the assert macro for
verification?
What about any other options?

Regards,
Angel Tsankov

Jan 24 '06 #1
14 2192
Angel Tsankov wrote:
I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:

#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
#else
#include <cassert>
#endif

Unfortunately, NDEBUG could be defined to something meaningful, in
which case it's value would be lost.

Can anyone think of some other way to use the assert macro for
verification?
What about any other options?

Regards,
Angel Tsankov


I have implemented my own macros, which are similar to Microsoft's.
Something like:

class MyException
{
MyException( const char* msg );
const char* what() const;
};

#define VERIFY(cond) if(cond); else throw MyException( #cond )
#if defined( NDEBUG )
# define ASSERT(cond) VERIFY(cond)
#else
# define ASSERT(cond)
#endif

Of course, you can make your VERIFY do something other than throw an
exception. Remember to use compile-time assertions wherever possible
instead of run-time assertions.

Cheers! --M

Jan 24 '06 #2

"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Angel Tsankov wrote:
I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should
be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:

#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
#else
#include <cassert>
#endif

Unfortunately, NDEBUG could be defined to something meaningful, in
which case it's value would be lost.

Can anyone think of some other way to use the assert macro for
verification?
What about any other options?

Regards,
Angel Tsankov


I have implemented my own macros, which are similar to Microsoft's.
Something like:

class MyException
{
MyException( const char* msg );
const char* what() const;
};

#define VERIFY(cond) if(cond); else throw MyException( #cond )
#if defined( NDEBUG )
# define ASSERT(cond) VERIFY(cond)
#else
# define ASSERT(cond)
#endif

Of course, you can make your VERIFY do something other than throw an
exception.


I want my VERIFY to display a message and abort the program. The
problem is where to display the message - send it to std::cerr, use a
message box, file or what? It would also be nice if the solution is
portable. The assert macro takes care of where to output the message
and is portable. That's why I'm considering it. However, as I
mentiond, there is a slight problem with it - its value can get lost
when I #undef it. So, I'm asking for ideas how to cope with this
problem. I'd also appreciate any other relevant ideas.

Jan 24 '06 #3
Angel Tsankov wrote:
"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
Angel Tsankov wrote:
I need to make verifications in release builds. If a verification
fails, an error message should be displayed and the program should
be
aborted. I need this solution to be portable. Then I thought of the
assert macro - it provides the desired functionality but only if
NDEBUG is not defined. So, I could write smth like this:

#if defined NDEBUG
#undef NDEBUG
#include <cassert>
#define NDEBUG
#else
#include <cassert>
#endif

Unfortunately, NDEBUG could be defined to something meaningful, in
which case it's value would be lost.

Can anyone think of some other way to use the assert macro for
verification?
What about any other options?

Regards,
Angel Tsankov


I have implemented my own macros, which are similar to Microsoft's.
Something like:

class MyException
{
MyException( const char* msg );
const char* what() const;
};

#define VERIFY(cond) if(cond); else throw MyException( #cond )
#if defined( NDEBUG )
# define ASSERT(cond) VERIFY(cond)
#else
# define ASSERT(cond)
#endif

Of course, you can make your VERIFY do something other than throw an
exception.


I want my VERIFY to display a message and abort the program. The
problem is where to display the message - send it to std::cerr, use a
message box, file or what? It would also be nice if the solution is
portable. The assert macro takes care of where to output the message
and is portable. That's why I'm considering it. However, as I
mentiond, there is a slight problem with it - its value can get lost
when I #undef it. So, I'm asking for ideas how to cope with this
problem. I'd also appreciate any other relevant ideas.


My relevant idea was given in the previous post. Just substitute your
desired behavior for the throwing of the exception. You could output to
std::cerr and call std::exit, for instance.

Cheers! --M

Jan 24 '06 #4

"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
Angel Tsankov wrote:
"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
> Angel Tsankov wrote:
>> I need to make verifications in release builds. If a
>> verification
>> fails, an error message should be displayed and the program
>> should
>> be
>> aborted. I need this solution to be portable. Then I thought of
>> the
>> assert macro - it provides the desired functionality but only if
>> NDEBUG is not defined. So, I could write smth like this:
>>
>> #if defined NDEBUG
>> #undef NDEBUG
>> #include <cassert>
>> #define NDEBUG
>> #else
>> #include <cassert>
>> #endif
>>
>> Unfortunately, NDEBUG could be defined to something meaningful,
>> in
>> which case it's value would be lost.
>>
>> Can anyone think of some other way to use the assert macro for
>> verification?
>> What about any other options?
>>
>> Regards,
>> Angel Tsankov
>
> I have implemented my own macros, which are similar to
> Microsoft's.
> Something like:
>
> class MyException
> {
> MyException( const char* msg );
> const char* what() const;
> };
>
> #define VERIFY(cond) if(cond); else throw MyException( #cond )
> #if defined( NDEBUG )
> # define ASSERT(cond) VERIFY(cond)
> #else
> # define ASSERT(cond)
> #endif
>
> Of course, you can make your VERIFY do something other than throw
> an
> exception.


I want my VERIFY to display a message and abort the program. The
problem is where to display the message - send it to std::cerr, use
a
message box, file or what? It would also be nice if the solution is
portable. The assert macro takes care of where to output the
message
and is portable. That's why I'm considering it. However, as I
mentiond, there is a slight problem with it - its value can get
lost
when I #undef it. So, I'm asking for ideas how to cope with this
problem. I'd also appreciate any other relevant ideas.


My relevant idea was given in the previous post. Just substitute
your
desired behavior for the throwing of the exception. You could output
to
std::cerr and call std::exit, for instance.


Output to std:cerr gets "lost" in GUI-based applications.

Jan 24 '06 #5
Angel Tsankov wrote:
"mlimber" <ml*****@gmail. com> wrote in message
My relevant idea was given in the previous post. Just substitute
your
desired behavior for the throwing of the exception. You could output
to
std::cerr and call std::exit, for instance.


Output to std:cerr gets "lost" in GUI-based applications.


The problem is not that cerr is not portable but that GUIs aren't.
Another portable alternative is writing to a log file, but personally,
I think throwing an exception that contains the error message (see my
first post) is the cleanest option. In such a case, you use the same
error reporting tool in the code, but supply an appropriate handler for
each platform, e.g.:

// For some GUI API
try
{
DoSomething();
}
catch( const MyException& e )
{
DialogBox( string("Error: ") + e.what() + " Terminating." );
exit( -1 );
}

// For some console application
try
{
DoSomething();
}
catch( const MyException& e )
{
cerr << ""Error: " << e.what() << " Terminating" << endl;
exit( -1 );
}

// For some program with no console attached
try
{
DoSomething();
}
catch( const MyException& e )
{
ofstream log( "log.txt", ios::app );
log << ""Error: " << e.what() << " Terminating" << endl;
exit( -1 );
}

Cheers! --M

Jan 24 '06 #6

"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
Angel Tsankov wrote:
"mlimber" <ml*****@gmail. com> wrote in message
> My relevant idea was given in the previous post. Just substitute
> your
> desired behavior for the throwing of the exception. You could
> output
> to
> std::cerr and call std::exit, for instance.
>


Output to std:cerr gets "lost" in GUI-based applications.


The problem is not that cerr is not portable but that GUIs aren't.
Another portable alternative is writing to a log file, but
personally,
I think throwing an exception that contains the error message (see
my
first post) is the cleanest option.


The reason why I'd rather avoid this approach is that I am concerned
about using it in dtors.

Jan 24 '06 #7
Angel Tsankov wrote:
"mlimber" <ml*****@gmail. com> wrote in message
The problem is not that cerr is not portable but that GUIs aren't.
Another portable alternative is writing to a log file, but
personally,
I think throwing an exception that contains the error message (see
my
first post) is the cleanest option.


The reason why I'd rather avoid this approach is that I am concerned
about using it in dtors.


Then, perhaps the best solution is an ostream-based log. You could
accept an ostream reference or (smart) pointer in your classes'
constructors, and they could use it to write their messages. Then on
different platforms, you could pass the ostream-type object (e.g.,
std::cerr, a std::ofstream for a log file, a std::stringstre am) that
best suits that platform.

Cheers! --M

Jan 24 '06 #8

"mlimber" <ml*****@gmail. com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
Angel Tsankov wrote:
"mlimber" <ml*****@gmail. com> wrote in message
> The problem is not that cerr is not portable but that GUIs
> aren't.
> Another portable alternative is writing to a log file, but
> personally,
> I think throwing an exception that contains the error message
> (see
> my
> first post) is the cleanest option.


The reason why I'd rather avoid this approach is that I am
concerned
about using it in dtors.


Then, perhaps the best solution is an ostream-based log. You could
accept an ostream reference or (smart) pointer in your classes'
constructors, and they could use it to write their messages.


Well, I'm sorry to tell you but I can see another flaw with your
suggestion - my class cannot have a default ctor.

Jan 24 '06 #9
Angel Tsankov wrote:
"mlimber" <ml*****@gmail. com> wrote in message
Then, perhaps the best solution is an ostream-based log. You could
accept an ostream reference or (smart) pointer in your classes'
constructors, and they could use it to write their messages.


Well, I'm sorry to tell you but I can see another flaw with your
suggestion - my class cannot have a default ctor.


So what's the problem? If you followed the plan mentioned above, there
would be no default constructor.

Cheers! --M

Jan 24 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3566
by: Eyal | last post by:
Hi, We have an issue with Debug Assertion showing in Release builds in Managed C++! I have created a small managed C++ project that looks like this: #include "stdafx.h" #using <mscorlib.dll> #using <System.dll> using namespace System; int _tmain()
5
2101
by: InstallAware | last post by:
For Press Inquiries: Sinan Karaca InstallAware Software Corporation 336 Guerrero Street, San Francisco CA 94103 415 358 4094 (voice/fax) sinank@installaware.com http://www.installaware.com/ InstallAware First (and Only) Installation Toolkit to Support .NET 2.0
12
6133
by: Vasco Lohrenscheit | last post by:
Hi, I have a Problem with unmanaged exception. In the debug build it works fine to catch unmanaged c++ exceptions from other dlls with //managed code: try { //the form loads unmanaged dlls out of which unmanaged exception //get thrown
5
5373
by: David++ | last post by:
Hi there, I have built a DLL in Visual C++ 6. When I build the DLL it builds fine for the debug version of the DLL (and this DLL works fine), however, I seem unable to build a Release version of the DLL. If I do a 'Batch Build' and select both Debug and Relase versions for building it will build the debug version but throws up errors for the Release version. For example, the output I get for a Batch Build is this - ...
3
2906
by: Steve Franks | last post by:
I'm using Visual Studio 2005 RC and cannot figure out how to produce a "release" build. Am I doing something wrong? I'm wondering if perhaps MS locked out the ability to produce a release build when using the RC since it does not have a go-live license? Or maybe I'm just doing something wrong. I do not want to go live with a release build. Rather I just want to test locally to see if there is a performance difference and also to...
2
1417
by: saju.prabhakaran | last post by:
Hi, I have an application in C++ which will report stack report. iam getting the report file in Debug mode. But when i build the apps in relaese mode , iam not getting the expected report. Pl help on this sam
1
3006
by: kurt sune | last post by:
I am having trouble publishing a website for RELEASE. 1. web.config: <compilation defaultLanguage="vb" debug="false"> 2. in Configuration manager I set the configuration to Release for the website 3. in Configuration manager I set the Active solution configuration to Release
6
9155
by: Andrew Rowley | last post by:
I am having trouble getting debug and release builds to work properly with project references using C++ .NET and Visual Studio 2003. I created a test solution, with a basic Windows form C++ project. I then add a class library, and add a reference to this project in the first project. When I do a release build, I see the following in the output from the DLL compile: /OUT:"C:\Documents and Settings\Andrew\My Documents\Visual Studio
0
1105
by: Duncan Smith | last post by:
Last week, I got reports back of errors in a patch I let someone try out (release dll, but not from the build machine). Turns out it was some Debug::Assert statements firing. In umanaged code, I'd always understood that assertions were only fired in debug builds and would compile down to NOPs in release builds (for good reasons) Now it looks as though I have to be careful to code assertions conditionally?
0
10134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11113
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9858
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7389
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6298
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4450
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3507
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.