Assert

Started by
13 comments, last by tuxx 21 years, 9 months ago
In my Direct3D application, instead of using the defensive if(FAILED(...))), I decided to use assert(...). This worked great for me in debug mode. However, as someone pointed out to me, ''...'' won''t get called in the release build because assert gets compiled out. Well, they''re obviously right. Is there a version of assert that is made for defensively calling functions, not calling a function on the return type and asserting the return type? If not, is there source code to assert, or an assert-like function? Or should I just bite the bullet and declare a glocal HRESULT and do something like:

assert(hr=g_pd3d->CreateDevice());
 
Or I could also just do something like:

#define Assert(exp) (HRESULT hr; assert(hr=exp))
 
What do yo guys think is the best way of going about this? Thanks!
[email=dumass@poppet.com]dumass@poppet.com[/email]
Advertisement
assert looked something like this in CBuilder4 (this is from memory, but I''m possitive it''s something like this):


  #ifndef NDEBUG#define assert(a) if(a) exit(0);else void;#else#define assert(a) void;#endif  


You could always define assert in a debug build manualy...


  #undef assert#define assert(a) if(a) exit(0); else void;  
hmmm... Thanks, I should try that. But wouldn't your code be something like:

    #ifdef NDEBUG#undef assert#define assert(a) if(!a) exit(0); else void;    

?

Well, anyways, I think I should try that. Thanks!

p.s.

Could I do this?

  #ifndef NDEBUG#undef assert#define assert(exp) (exp)#endif  

? That way I could just execute the expression

[edited by - tuxx on July 18, 2002 2:30:52 PM]
[email=dumass@poppet.com]dumass@poppet.com[/email]
You''re using assert wrong... It should halt for errors in your code, not errors your graphics card might give you...
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
MFC''s VERIFY does what you want, and the source is available

Cheers
Chris
quote:Original post by Gee
You''re using assert wrong... It should halt for errors in your code, not errors your graphics card might give you...


Errors are errors. If a function can return an error condition, it''s perfectly acceptable to use assert on it. I suggest you do more reading on what assertions are, and what they can be used for.

One thing, tho; if a function has a non-zero error condition, the assertion should check for equality to the success condition.


Don''t listen to me. I''ve had too much coffee.
You''re right! I''m still going to use assert though (even if it''s wrong ), because it provides information on the line that backfired (it beats "Couldn''t ").
[email=dumass@poppet.com]dumass@poppet.com[/email]
Wow I'm gettin a lot of replies... That last message was in reply to the first poster. I'll try lookin up verify!

edit: Chris: How do I find the sources?

[edited by - tuxx on July 18, 2002 2:56:51 PM]
[email=dumass@poppet.com]dumass@poppet.com[/email]
Well, either we disagree on the point of assert, or I have missunderstood what hr=g_pd3f->CreateDevice() can return.

But I still say if you see an assertion you have a bug in your code, not errors that can vary on different computers and hardware profiles... And if you suggest I should go and "read on what assertion is", then I''ll tell you that''s what I''ve read!

Maybe it''s my sources that''s been flawned, and if I''m wrong, do correct me...

/G
---GUI Programming Division Manager at Wildfire Gamesworking on the 0 A.D. project
I side with Gee. You should not use assert for things that could very well happen when the program is in the hands of end users. The purpose of using assert is to place sanity checks in your own code to find your own logic errors.

This is not to say that you should not have any error-handling code in your release program, but merely that you need to use a different error-handling mechanism. There are far more elegant ways to handle errors than simply halt the program and report some obscure register values to the end user.

This topic is closed to new replies.

Advertisement