Cocos2D-x 3.5 engine resolution-reference count (Ref), PoolManager, AutoreleasePool, cocos2dref
# Include <CCRef. h>
Ref is used for reference count manangement. If a classinherits from Ref.
Class Ref is a reference counting Class used to manage the reference counting of objects. In this way, no pointer points to this object. When this pointer is used, a program exception occurs if the object to which it points is destroyed.
Class CC_DLL Ref
{
Public:
Void retain (); // Add reference count once
Void release (); // reduce the reference count once. If the reference count is zero at this time, use AutoreleasePoolManager to release the memory.
Ref * autorelease (); // Add the current object to the Auto Release pool to make it an object that can use reference count
For example:
PhysicsBody * PhysicsBody: create ()
{
PhysicsBody *Body= New (Std::Nothrow) PhysicsBody ();
If (Body&&Body-> Init ())
{
Body-> Autorelease (); // here, you can use autorelease to add the body to the Auto Release pool to make the body an object that can be referenced and counted.
ReturnBody;
}
CC_SAFE_DELETE (Body);
Return nullptr;
}
Unsigned int getReferenceCount () const; // return the reference count value
Protected:
Ref (); // the initial reference count is 1.
Public:
Virtual ~ Ref ();
Protected:
Unsigned int _ referenceCount; // reference count
Friend class AutoreleasePool // Automatically releases the pool friend class
};
PoolManager is the resource pool manager that manages a vector that automatically releases the pool. Designed in the singleton mode, that is, only one PoolManager object is allowed.
# Include <CCAutoreleasePool. h>
Class CC_DLL PoolManager
{
Public:
CC_DEPRECATED_ATTRIBUTEstatic PoolManager * sharedPoolManager () {return getInstance ();} // The static method returns the pointer of the singleton object
Static PoolManager * getInstance (); // use this function to return a pointer to a single-instance object. Here, check whether s_singleInstance is null. If it is null, a new PoolManager object is created.
CC_DEPRECATED_ATTRIBUTEstatic void purgePoolManager () {destroyInstance ();} // analyze the PoolManager pointer
Static void destroyInstance ();
AutoreleasePool * getCurrentPool () const; // returns the last automatically released pool object in the vector.
Bool isObjectInPools (Ref *Obj) Const; // check whether there are obj objects in the automatically released pool
Friend class AutoreleasePool; // sets the automatic release pool to a friend class.
Private:
PoolManager (); // constructor. The initial vector can contain ten automatically released pools.
~ PoolManager ();
Void push (AutoreleasePool *Pool); // Adds the pool from the back to the vector
Void pop (); // The back element in the vector is displayed.
Static PoolManager * s_singleInstance; // single-instance object pointer, set as private Element
Std::Vector<AutoreleasePool *> _ releasePoolStack; // The automatically released pool vector managed by this class
};
The Auto Release pool manages a vector that references counting objects, and each Auto Release pool has its own name.
# Include <CCAutoreleasePool. h>
Class CC_DLL AutoreleasePool
{
Public:
AutoreleasePool (); // constructor. The name of the automatically released pool during initialization is null, and the vector capacity is 150.
AutoreleasePool (constStd::String&Name); // Constructor. The name of the automatically released pool during initialization is name, and the vector capacity is 150.
~ AutoreleasePool (); // destructor
Void addObject (Ref *Object); // Add the object from the back to the vector
Void clear (); // clears the vector
Bool contains (Ref *Object) Const; // check whether the vector contains an object
Void dump ();
Private:
Std::Vector<Ref *> _ managedObjectArray; // vector object
Std::String_ Name; // name of the automatically released pool
};