Thursday, October 31, 2013

Cocos2D-X: Useful Code Saving Memory Management Macros


CC_SAFE_RELEASE: Checks whether the object is valid before calling "release" on it.

CC_SAFE_RELEASE_NULL: Checks whether the object is valid before calling "release" on it, and then also sets the variable to NULL.

CC_SAFE_RETAIN: Use when it's possible that the return value from a function can be NULL and you don't want to call retain on a NULL value.


#define CC_SAFE_RELEASE(p)            do { if(p) { (p)->release(); } } while(0)
#define CC_SAFE_RELEASE_NULL(p)        do { if(p) { (p)->release(); (p) = 0; } } while(0)
#define CC_SAFE_RETAIN(p) do { if(p) { (p)->retain(); } } while(0)

No comments:

Post a Comment