Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

tuxx

Assert

This topic is 5817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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;

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
You''re using assert wrong... It should halt for errors in your code, not errors your graphics card might give you...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
MFC''s VERIFY does what you want, and the source is available

Cheers
Chris

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ").

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!