Inlining SAFE_RELEASE()?

Started by
4 comments, last by Goblin 22 years, 8 months ago
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
- The Goblin (madgob@aol.com)
Advertisement
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... --
// Javelin// Assumption is the mother of all fuckups...
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
We are boki. The rest is known.
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!"
- The Goblin (madgob@aol.com)
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.
The better way would be to use the _com_ptr_t class defined in COMDEF.H or an equivalent class.

Bjørn.
We are boki. The rest is known.

This topic is closed to new replies.

Advertisement