Archived

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

Goblin

Inlining SAFE_RELEASE()?

Recommended Posts

I'm just curious how I would go about turning this ever-so-useful DirectX macro into an inline function...
    
#define SAFE_RELEASE(p) { if ( (p) ) { (p)->Release(); (p) = 0; } }
    
Thanks, appreciated! ----------------- The Goblin (madgob@aol.com) ----------------- "Before critisizing somebody, walk a mile in their shoes. That way, when you do critisize them, not only will you be a mile away, but you'll also have their shoes!" Edited by - Goblin on August 7, 2001 2:12:04 PM

Share this post


Link to post
Share on other sites
May I ask why you want it as an inline function...? I don''t think you can make it any faster than it already is...

// Javelin
-- Why do something today when you can do it tomorrow... --

Share this post


Link to post
Share on other sites
Hm, to do SAFE_RELEASE as an inlined function, one has to pass a pointer to a pointer to be able to set it to NULL. Sp the function may look like this (not tested nor used):

  
template < class T >
void SafeRelease( T** p )
{
if ( *p != NULL )
{
(*p)->Release();
(*p) = NULL;
}
}

or
  
void SafeRelease( IUnknown** p )
{
if ( *p != NULL )
{
(*p)->Release();
(*p) = NULL;
}
}


BTW: I agree with Javelin. There is actually no reason to do so.

Bjørn.

Edited by - Boki on August 7, 2001 4:34:07 PM

Share this post


Link to post
Share on other sites
Well, I was under the impression that it is better to use inline, constant, and enum in favor of #define.

I suppose because I figured if I''m going to program in C++ over C, may as well go all the way, right?

-----------------
The Goblin (madgob@aol.com)
-----------------
"Before critisizing somebody, walk a mile in their shoes. That way, when you do critisize them, not only will you be a mile away, but you''ll also have their shoes!"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
There is a reason to use an inline version of the function instead of a macro: typechecking! The object code created will be the same (maybe not in all cases) but you get compile time errors as opposed to runtime crashes.

Share this post


Link to post
Share on other sites
The better way would be to use the _com_ptr_t class defined in COMDEF.H or an equivalent class.

Bjørn.

Share this post


Link to post
Share on other sites