Friendly note: Don't ASSERT a function call.

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
So here I am, finishing up a project. It works flawlessly. I'm happy, I spent 4 hours today writing up a developer document for the API and interface. In the process of getting everything packaged up I recompile everything in release just to make sure the static library I wrote links in fine with both my test utility as well as the sample COM library.

Proceeding to test, the release test client crashes miserably. :( Okay, make sure the right COM library got registered, and try again. CRASH. Hrmph. Change the link library settings on my static library, recompile, copy, recompile the test client and COM library. It's good!

Except... why are none of my object properties returning anything? Check settings... recompile, re-register, try again. Nope, still nothing. Recompile everything in debug, works fine. Client in release, COM in debug, works fine. Hrm...

So I take a quick look at my COM code. For some reason, somehow I thought it would be a good idea to use the following syntax when calling the initialization functions:

ATLASSERT(SUCCEEDED(FunctionCall()));

Ehhem.. yeah. Okay... um... :music:/whistles innocently:music:

Have a great weekend ladies and gents!

Edit: In case any of the younger programmers are wondering why this is a big deal. That line of code (the entire line) in release mode evaluates to "noop". :eek:
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Done that.

And since you only single-step in DEBUG builds, it can be a little hard to find the problem that way :)
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Most of my code uses something of the form:
#ifndef DEBUG
#define ASSERT(x) x
#endif

... and thusly side effects remain even when not in debug mode.
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Originally posted by: postmortemIA
i have yet to use assert, for me is like coder is unsure if his code works at all.

I did it in my code simply because it was a sample application, I'm not very familiar with ATL and COM in general, and I wasn't using exception handling for those certain blocks of code. Just checking the HRESULT was fine for testing purposes.

Still, it didn't register in my head that that was how ASSERTs evaluated until I saw the macro expansion in release mode show "noop". :eek:
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: postmortemIA
i have yet to use assert, for me is like coder is unsure if his code works at all.

What if you're writing a library, and need to assert that some unknown future caller is passing arguments that have compliant values?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: Markbnj
Originally posted by: postmortemIA
i have yet to use assert, for me is like coder is unsure if his code works at all.

What if you're writing a library, and need to assert that some unknown future caller is passing arguments that have compliant values?

if (invalid entry)
return ERR_INVALID_ARG

 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
Originally posted by: postmortemIA
i have yet to use assert, for me is like coder is unsure if his code works at all.

How about in writing tests?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Originally posted by: EagleKeeper
Originally posted by: Markbnj
Originally posted by: postmortemIA
i have yet to use assert, for me is like coder is unsure if his code works at all.

What if you're writing a library, and need to assert that some unknown future caller is passing arguments that have compliant values?

if (invalid entry)
return ERR_INVALID_ARG

I much prefer an exception-based model, and assert fits in perfectly with that.