Projects > cpp > Issues > Refactor #3519

We are migrating issue tracker of Cocos2d-x Project to Github, please create new issue there. Thanks.

Create Issue on Github

Move the non-reference counted stuff out of Object

Refactor #3519 [Closed]
dumganhar 2013-12-27 03:17 . Updated over 2 years ago

My Email:

Indeed, I agree to move `autorelease` stuff outside of Object.
Now, in our Object class, there are some other variables and methods not refer to reference counting.

class CC_DLL Object
{
public:
    unsigned int        _ID;         >   Used only for Lua bindings
    int                 _luaID;      >   Used only for Lua bindings
protected:
    /// count of references
    unsigned int        _reference;     —> (1) Used for reference counting
    /// count of autorelease
    unsigned int        _autoReleaseCount;    >  Used for autorelease pool 
public:
    Object();
    virtual ~Object();

    inline void release()    —>  (1)
    inline void retain()     —>  (1)

    Object* autorelease();  >  used for autorelease pool

    bool isSingleReference() const;    —>  (1)
    unsigned int retainCount() const;  —>  (1)

    virtual bool isEqual(const Object* object);              >  should not be in Object
    virtual void acceptVisitor(DataVisitor &visitor);        >  should not be in Object
    virtual void update(float dt) {CC_UNUSED_PARAM(dt);};    >  should not be in Object
};

We should only keep (1) , move all others out of our Object.

Problem:

There will not autorelease() method for all objects. So the old code will not be able to work.

Sprite* Sprite::create(const std::string& filename)
{
    Sprite *sprite = new Sprite();
    if (sprite && sprite->initWithFile(filename))
    {
        sprite->autorelease();   // Broken here.
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}

My Idea:

Since autorelease is just a method that adding an object to autorelease pool.

Object* Object::autorelease()
{
    PoolManager::sharedPoolManager()->addObject(this);
    return this;
}

So probably, we could define a macro like:

#define CC_AUTORELEASE_OBJECT(object) \
    PoolManager::sharedPoolManager()->addObject(object);

Therefore, the Sprite::create will be :

Sprite* Sprite::create(const std::string& filename)
{
    Sprite *sprite = new Sprite();
    if (sprite && sprite->initWithFile(filename))
    {
        CC_AUTORELEASE_OBJECT(sprite);
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return nullptr;
}

Yep, in fact, we don’t encourage developer to use `autorelease` stuff in their game codes. It’s hard to detect where to cause crash since autorelease delay the release operation.

We encourage them to write codes like:

MySprite* sp = new MySprite();
sp->init();

xxx

sp->release();
walzer@cocos2d-x.org 2014-01-08 08:30
  • Target version changed from 88 to 34
walzer@cocos2d-x.org 2014-05-04 07:20
  • Description updated (diff)
  • Status changed from New to Closed
  • Target version changed from 34 to 3.0-beta2

We've fixed this by the new cocos2d::Ref

Atom PDF

Status:Closed
Start date:2013-12-27
Priority:Normal
Due date:
Assignee:dumganhar
% Done:

0%

Category:all
Target version:3.0-beta2

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.