Preface:
In C ++, the methods for allocating dynamic memory on the stack and releasing dynamic memory are new and delete. After applying for the memory, if it is not used, delete it, otherwise, it will cause memory overflow. Appendix: the new dynamic memory only needs to be deleted before the program ends, so that the applied heap memory can be returned to the system before the program ends, however, in the actual programming process, in order to release unused space in time and improve memory usage, you can call delete to release it in an unwanted location, so as not to cause high memory usage and forgetting before the end of the program, resulting in memory overflow.
In the cosos2d-x, for the engine itself, the autorelease method is provided to add the object pointer to the Auto Release pool. Call pop at the end of each frame to exit the automatic release pool for the object pointer in the pool, and release the pool automatically based on the reference count. After reading blog articles for a few days and "Understanding", I found myself confused.
Again from the beginning, and then from the source code itself to understand, because it is in the magic tower DEMO debugging when a lot of memory leakage prompt (vs2010 + cocos2d-x 2.1.3 + VLD ), in the spirit of resolutely digest and absorb the memory management method in the cocos2d-x, repeated debugging many times, now to make a summary.
First, the cocos2d-x officially recommended way to generate objects is to use CREATE_FUNC macro, macro has an autorelease () call, so you only need to directly className * pObject = className: create (), you can just call the CCObject class directly or indirectly if you need to use the memory automatic management function. After you add pObject, you do not need to release the pointer of the object. Of course, pObject-> retain () (you need to execute pObject-> release ()) operation. It is easy to manage the memory that needs to be allocated on the stack. We recommend that you use autorelease () to manage the memory release.
Second, for the allocated memory to have pointers to the current class data member and local objects in the local scope, the class data member directly autorelease (). At the end of the class lifecycle, the class destructor does not need to be released again, because the engine Automatically releases the part object pointer and does not need to be released again after autorelease () is called, all of them are handed over to the engine for release.
Finally, for manual operations, the new operation and delete operation appear in pairs, and the class data member is deleted in the destructor to avoid Memory Operation errors due to release in advance. Furthermore, if you execute className * pObject = new className (); pObject-> autorelease (), you can also use the engine's memory management function, provided that className is directly or indirectly inherited from the CCObject class.
The first write cocos2d-x memory management experience, please look at the play, refer to a lot of great god memory management blog, which due to their own level is limited, failed to understand, but to their own dizzy, I am sorry for your hard work and thanks again for sharing the blog ..
I hope this article can be used as a reference for anyone who needs it...