release and retain with member variables

release and retain with member variables
0.0 0


I’m not quite understand about the release and retain when use member variables.
For example:
class A : public CCLayer
A ();
virtual ~A();
virtual bool init();
CCString *mStr;

A::A ()
bool A::init()
bool bRet=false;


return bRet;

Now I have the question that do I need to call “mStr->release()” int the A\ function?
In\ fact\ I\ have\ tried\ runing\ the\ program\ without\ calling\ the\ “mStr->release()”\ int\ the
A function and got no errors.
I don’t know whether this is right,could somebody please help me?


Simple answer, yes you need to call release on any CCObject you retain explicitly. Otherwise you won’t get an error but you will get a memory leak. This is not about member variables, this is about how the engine refcount system works.

Best way to find the answer and to learn is to grab a coffee, go out there and use breakpoints. Have you tried putting one in the CCString destructor? What happens when you create a CCString and don’t call retain on it? What does the callstack looks like? Whad happens if you do call retain? Does the CCString destructor ever gets called upon program termination in that case?

Have fun and good luck!


Very exhaustive,now I get the point,thank you very much!