Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows "Asserts"

Status
Not open for further replies.

Simse

Programmer
Feb 24, 2005
22
Hi all,
I have a question about Asserts. Is there any
way to get the information from the Asserts like
line-number, file, .. where an error occured?
My Problem is, that I have a programm, that shouldnt
stop on an assert, because I need to handle some
stuff before I close the programm, otherwise bad things
could happen :)
So I want to catch an Assert when it occures, save
line an file, and close my programm the way I want
to close it.

Would be very happy for some help.

Cheers
Simon
 
Some more information :)
I sure new, that i could stop asserts by
building a release, but i need the debug one.
And another point is, that i want the information
from the assert to find my errors.

cheers again
simon
 

You could add TRACE statements so you know what part of your code you're in, let the assert fail (while still in debug mode) and an MFC error should appear with what ASSERT is failing.

Code:
//TRACE with string substitution
TRACE( _T("OH MY GOD I FAILED AT %s"), strClassName );

Or you could add source info (like line number of what ever class it's in). Usually WINDOW ASSERTS come with the source info so if you're just trying to find out where it's happening then I'd add these until you find out what's the bug.
Hope this helps.
 
As far as I know - no legal (and portable) method to catch assertion from <cassert> macro mechanics.

Sometimes it's possible. For example, in VC++ 6.0 you may replace RTL function _assert (see <assert.h> header), for example (console application) add this module to your project:
Code:
// Dirty trick - RTL _assert function replaced.
#include <iostream>
using namespace std;

extern "C"
void _assert(void* expr, //< expression (textually)
             void* file, //< file (module) path
             unsigned lineno //< line number
            )
{ // Don't be shy, do what you need here:
  cerr << "*** assert(" 
       << (expr?(const char*)expr:"Unknown expression")
       << "): "
       << (file?(const char*)file:"Unknown file")
       << " line # " << lineno << endl;
  // Back to the crash point? Better exit or what else...
}
See also __FILE__ and __LINE__ macros (defined in C and C++ Standards).

Of course, if you may define your own assertion mechanics then use it from the beginning of the project - it's not a dirty trick but creative approach...
 
When I write an assert, I usually add info to it that describes what happened or where:
Code:
assert( (pArg != NULL) && "pArg in Func1() is NULL!" );

Another way is to write your own Assert() function like this:
Code:
#ifdef DEBUG
void Assert( bool  condition,
      const char*  file,
              int  line )
{
   if ( condition == false )
   {
      cout << "Assert failure in: " << file
           << " on line: " << line << endl;
      exit( 1 );  // Remove if you don't want it to exit after an assert.
   }
}
#else
#   define Assert( blah1, blah2, blah3 )
#endif

...

Assert( (pArg != NULL), __FILE__, __LINE__ );
 
> that I have a programm, that shouldnt stop on an assert,
assert() should mean that the program has a fault that has no possibility of recovery. In other words, a bug caused by something the programmer has not anticipated.

It seems to me you should be using exceptions (which can be caught at some point higher up the call stack) to handle these cases.

> And another point is, that i want the information
> from the assert to find my errors.
I thought windows (in the form of Dr. Watson) saves a bunch of information to a file at the point an application dies.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top