English 中文(简体)
cocos2d-x 中的正确即时和内存管理
原标题:proper instantiation & memory management in cocos2d-x

I ve been looking for documentation for cocos2d-x but it seems to be really really poor beyond the very basics. I understand that my own classes should inherit from CCObject to be able to use (originally cocoa s) retain/release mechanism, but I m still confused about what happens when you new something. init is not called automatically. is it OK to call it from inside the constructor? does that alone guarantee that my object will start with a reference count of 1? what is CC_SAFE_DELETE and when should I use it? do release and autorelease work exactly like in cocoa? what about CC_SYNTHESIZE? I just need to see a properly coded class example (and it s instantiation/destruction) to understand what I m supposed to do so as not to screw and leave memory leaks. thank you.

最佳回答

如果您要查看 CCObject 类的代码, 您可以看到其中的构建器引用计数设置为 1 。 因此, 使用 < code> new 创建对象是正确的。 Init 不调用, 因为 CCObject 类没有这样的方法 。 通常我更喜欢使用静态构建器创建对象 。 Smth like

MyClass* MyClass::createInstance()
{
    MyClass* object = new MyClass();

    // you can create virtual init method
    // and call it here

    if( initWasSuccessful )
    {
        object->autorelease();
    }
    else
    {
        CC_SAFE_RELEASE_NULL(object);
    }
    return object;
}

关于所有宏,比如 CC_SAFE_DELETE - 您可以在 cocos2dx 代码中找到它们。 这个宏只是检查对象是否为 NULL 以防止在尝试调用释放方法时发生崩溃 。

问题回答

莫里昂的回答很好,我只想就这个问题补充几个有用的联系。

Here you can find the official memory management in Cocos2d-x page: Memory Management in Cocos2d-x

This forum page also contains some more details and clarifications about it: Memory deallocation in Cocos2d-x

玩得开心点!





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签