Cocos2d-x 3.5 Engine Resolution-reference count (REF), self-release pool (Poolmanager), self-release pool manager (Autoreleasepool)

Source: Internet
Author: User

#include <CCRef.h>

REF is used for reference count Manangement. If a classinherits from Ref.

Class Ref is a reference count class that is used to manage the reference count of objects.

This does not occur and the pointer remains pointing to the object, and when the pointer operation is used, it is assumed that the object pointed to is destroyed and a program exception occurs.

class Cc_dll Ref

{

Public:

void retain ();//Add Reference count once

void Release ();//Decrease the reference count once, assuming that the reference count is zero at this time, use autoreleasepoolmanager to free up memory

Ref* autorelease();//Add the current object to your own active release pool. Make it an object that can use a reference count

Like what:

physicsbody* physicsbody::Create()

{

physicsbody* Body = new (std::nothrow)physicsbody();

if (body &&body,init())

{

Body---autorelease();//use autorelease to increase the body to its own active release pool, you can make the body a reference to the object can be counted

return body;

}

Cc_safe_delete (body);

return nullptr;

}

unsigned int Getreferencecount () const;//return reference count value

protected:

Ref ();//Initialize reference count to 1

Public:

virtual ~Ref();

protected:

 

unsigned int _referencecount;//reference count

friend class autoreleasepool//voluntarily release Chiyu class

};

Poolmanager is the resource pool manager. Manages a vector that actively releases the pool itself. The use of a single-case pattern design. That means just agreeing to a Poolmanager object.

#include <ccautoreleasepool.h>

class Cc_dll Poolmanager

{

Public:

Cc_deprecated_attribute Static Poolmanager*sharedpoolmanager() { return getinstance();} static method returns a pointer to a singleton object

Static Poolmanager* getinstance();//Use this function to return a pointer to a singleton object, where the s_singleinstance is checked for null, and the new Poolmanager object is empty

Cc_deprecated_attribute Static void Purgepoolmanager () { destroyinstance();} destructor Poolmanager pointer

Static void destroyinstance ();

autoreleasepool *getcurrentpool()const;//Returns the last self-release pool object in the vector

BOOL Isobjectinpools (Ref* obj) const;//Check if you are actively releasing the pool for obj objects

friend class autoreleasepool;//Set your own active free pool as a friend class

Private:

Poolmanager ();//constructor. Initialize the vector to include 10 self-releasing pools of their own initiative

~Poolmanager();

void Push (autoreleasepool *pool);//Add pool from back to vector

void Pop ()//popup vector in the back element

Static Poolmanager* s_singleinstance;//Singleton object pointer, set to private element

std::vector<autoreleasepool*>_releasepoolstack;//The class manages its own active release pool vector

};

Own active Release pool manages a vector of reference counting objects, and each self-release pool has its own name

#include <ccautoreleasepool.h>

class Cc_dll Autoreleasepool

{

Public:

Autoreleasepool ();//constructor, initialize itself active release pool name is empty, vector capacity is 150

Autoreleasepool (conststd::string &name); constructor, initialize your own active release pool named Name,vector capacity of 150

 

~Autoreleasepool();//destructor

void AddObject (Ref *object);//Add object objects from back to vector

void Clear ();//Empty vector

BOOL contains (Ref* object) //Check if the vector contains object objects

void Dump ();

Private:

std::vector<Ref*>_managedobjectarray;//vector Object

std::string_name;//voluntarily release pool name

};

Cocos2d-x 3.5 Engine Resolution-reference count (REF), self-release pool (Poolmanager), self-release pool manager (Autoreleasepool)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.