What is the common pattern to derive from Ref without leaks?
Is the following snippet safe?
class Derived : public cocos2d::Ref
{
public:
Derived () : x(0), y(0), z(nullptr) {}
size_t x;
size_t y;
cocos2d::Sprite* z;
};
Should I add destructor to Derived as well:
~Derived ()
{
if (this->getReferenceCount() > 0)
this->release();
}
Hi @Greenhouse
You can have the destructor for Derived but don’t need to check the reference count, it will be added to Autorelease Pool.
So, no need to manually remove the reference.
Automatically? Even without a call to this->autorelease() before?
Or you mean with adding static Derived* create ()?
Derived* Derived::create ()
{
auto obj = new (std::nothrow) Derived();
if (obj)
{
obj->autorelease();
}
else
{
delete obj;
obj = nullptr;
}
return obj;
}
Not automatically.
You have to add it to the reference inside the create() as you have written.
Thanks for clarification.