Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

CrossApp memory management


May 21, 2021 CrossApp



CrossApp manages memory objects in a reference counting way, through CAObject, CAAutoreleasePool, and CAPoolManager.

CAObject is a reference count class, with a reference count of 1 at the time of its constructor, but is not added to the auto-release pool at this time, and all auto-reference counts are 0
When you create an object using create, autoelease is called and placed in the object pool, which is managed in the queue of CAAutoreleasePool. CAObject calls retain() and the reference count increases by 1 and the call release() reference count decreases by 1.

CAAutoreleasePool is an engine initialization that creates a default list of auto-release objects and adds them to CAPoolManager for management.

Each time the engine is in the main loop, there is a cleanup of the pool in CAPoolManager, and those CAObjects with a reference count of 0 are released. This is inside CrossApp

storage management mechanism.


CAObject

//属性:
//引用数量
unsigned int m_uReference;
 
//自动引用数量
unsigned int m_uAutoReleaseCount;
//方法:
//引用计数+1
retain();
 
//引用计数-1
relesase();
 
//添加到自动释放管理
autorelease();
 
//返回当前的引用计数
retainCount();

At the same time, the engine uses some functions and macro definitions about memory management to facilitate memory management.


Common functions
create() ; Contains autoelease() calls
insertSubview(); Contains the retain() call
removeSubview(); Contains a relesase() call


Common macros
#define CC_SAFE_DELETE(p) do { if(p) { delete (p); ( p) = 0; } } while(0)
#define CC_SAFE_DELETE_ARRAY(p) do { if(p) { delete[] (p); ( p) = 0; } } while(0)
#define CC_SAFE_FREE(p) do { if(p) { free(p); ( p) = 0; } } while(0)
#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)


There are also data containers specifically designed to accommodate CrossApp: CAVector, CAList, CADeque, AND CAMap are used in a similar way to the use of vector, list, deque, map, which is fundamentally different in that when it is added and removed, it follows CrossApp's memory management principles.


If we need to manually manage the memory of an object pointer ourselves, then we need that retain() and relesase () appear in pairs in a class, following the principle of who is responsible for retain and who is responsible for release.