Cocos2d Ref class and memory management usage

Source: Internet
Author: User
Tags addchild
I. INTRODUCTION

Friends who have written programs in C ++ and JAVA will be annoyed by the memory management mechanisms in different languages. The JAVA program runs on the JVM and the JVM automatically implements memory management. Developers only apply for memory instead of manually releasing the memory. When an object in JAVA is not referenced by any referenced variable (pointers similar to C and C ++), JVM releases the object. Like C ++, C ++ is a language that can be directly executed by the operating system after Compilation. No virtual machine is responsible for memory management. Therefore, you need to manage the memory in the program. This article describes how to use the memory management mechanism provided by cocos2d.

Cocos2d-x draws on the concept of "reference counting" to realize automatic memory management to a certain extent. The cocos2d delegated object management pool PoolManager stores objects and releases object resources. The criterion for releasing objects in the management pool is that the reference count of objects is 0.

II. Ref class

The Ref class implements interfaces for interaction with the cocos2d memory management mechanism. The memory of all Ref subclass objects can be automatically managed by the management pool. The Ref class interface is as follows:

//////////////////////////////////////// This method can increase the reference count of the Ref object void retain (); /// // reduce Ref object references count, // if the count is reduced to 0, the object is released void release (); /// // the reference count is not immediately reduced, // reduce the Ref object reference count when the current pool is ended. // if the count is reduced to 0, the object is released void autorelease ();

When instantiating a subclass of the Ref class, the retain method of the object can be called to increase the count by 1. Next, let's take a look at the implementation principle of the create method of the common Ref subclass Scene class:

Scene * Scene: create () {Scene * ret = new (std: nothrow) Scene (); if (ret & ret-> init ()) {ret-> autorelease (); return ret;} else {CC_SAFE_DELETE (ret); return nullptr ;}}

After the Scene class is instantiated, call the autorelease method immediately. If the returned ret is used elsewhere (that is, call the retain method of the object to add the reference count to 1 ), the object will not be destroyed after the pool ends. If the returned ret is not used, the object will be destroyed after the poo pool ends to prevent memory overflow.

//////////////////////////////////////// // The returned ret is used by the addChild method. // The addChild method calls the ret retain method to add the reference count to 1. // after the pool ends, the ret pointing to the object will not be released this-> addChild (Scene:: create (); // create returns the ret pointer

Here are some examples.

//////////////////////////////////////// /_ Array is a subclass of Ref. // The retain method must be explicitly called, otherwise, an error occurs in the count method. // because the array reference count is 0, the pool is released after the pool ends. auto array = _ Array: create (); array. retain ();.... array. count (); // after the last pool, for example, in the key callback
//////////////////////////////////////// The/addObject method can explicitly call sprite. retain (), // so the object will not be released Sprite * sprite = Sprite: create (); array. addObject (sprite );.... sprite * sp = (Sprite *) array. getLastObject (); // after the last pool, for example

The following summarizes some cocos2d memory management rules.

  • When using a Node object, the addChild () method can call the retain method of the added Node implicitly. The removeChild () method can call the release method of the removed object.
  • If it is a coco2d class, such as _ Array and _ Dictionary, they are child classes of Ref. When they add or delete elements, they can call the retain and release methods implicitly. However, the premise is that the objects __array and _ Dictionary must be retaken to ensure that they are not released.
  • If it is not the above Ref subclass object, you need to explicitly and call the retain and release methods in pairs.

Cocos2d Ref class and memory management usage

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.