Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference SAFE_RELEASE(), SAFE_DELETE()

Tags:

c++

c

directx

I'm newbie in programming.

I'm wondering release and delete function.

When I allocate memory with new, I should terminate it with delete.

But when I should use release?

What's the difference release and delete...?

like image 212
huooo Avatar asked Aug 05 '26 08:08

huooo


1 Answers

If you are looking for definition you will find

#define SAFE_RELEASE(p) { if ( (p) ) { (p)->Release(); (p) = 0; } }
#define SAFE_DELETE(a) if( (a) != NULL ) delete (a); (a) = NULL;

SAFE_DELETE should be used for memory allocated with new

SAFE_RELEASE should be called for com objects (like directx objects) and "under the hood" is doind something like this

if (--ref_cnt==0)
{
   delete this;
}

it decrements a reference counter and releases the object if there are no more references to it.

like image 133
cprogrammer Avatar answered Aug 06 '26 22:08

cprogrammer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!