2014-01-21 17:47:59 +08:00
|
|
|
#include "ReleasePoolTest.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
|
|
|
class TestObject : public Object
|
|
|
|
{
|
|
|
|
public:
|
2014-01-22 10:17:42 +08:00
|
|
|
TestObject() : _name(""){}
|
2014-01-21 17:47:59 +08:00
|
|
|
|
2014-01-22 10:17:42 +08:00
|
|
|
TestObject(std::string name) : _name(name)
|
|
|
|
{
|
|
|
|
CCLOG("TestObject:%s is created", _name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
~TestObject()
|
|
|
|
{
|
|
|
|
if (_name.size() > 0)
|
|
|
|
CCLOG("TestObject:%s is destroyed", _name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string _name;
|
2014-01-21 17:47:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void ReleasePoolTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
// title
|
|
|
|
auto label = LabelTTF::create("AutoreasePool Test", "Arial", 32);
|
|
|
|
addChild(label, 9999);
|
|
|
|
label->setPosition(Point(VisibleRect::center().x, VisibleRect::top().y - 30));
|
|
|
|
|
|
|
|
// reference count should be added when added into auto release pool
|
|
|
|
|
2014-01-22 10:17:42 +08:00
|
|
|
TestObject *obj = new TestObject("my test object");
|
2014-01-21 17:47:59 +08:00
|
|
|
obj->autorelease();
|
2014-01-22 10:17:42 +08:00
|
|
|
assert(obj->retainCount() == 1);
|
2014-01-21 17:47:59 +08:00
|
|
|
|
|
|
|
// can invoke autorelease more than once
|
|
|
|
obj->autorelease();
|
2014-01-22 10:17:42 +08:00
|
|
|
assert(obj->retainCount() == 1);
|
2014-01-21 17:47:59 +08:00
|
|
|
|
|
|
|
// create an autorelease pool in stack
|
|
|
|
|
|
|
|
{
|
|
|
|
AutoreleasePool pool1;
|
|
|
|
|
|
|
|
obj->autorelease();
|
2014-01-22 10:17:42 +08:00
|
|
|
assert(obj->retainCount() == 1);
|
2014-01-21 17:47:59 +08:00
|
|
|
|
|
|
|
// retain, release can work together with autorelease pool
|
|
|
|
obj->retain();
|
2014-01-22 10:17:42 +08:00
|
|
|
assert(obj->retainCount() == 2);
|
2014-01-21 17:47:59 +08:00
|
|
|
obj->release();
|
2014-01-22 10:17:42 +08:00
|
|
|
assert(obj->retainCount() == 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// pool is destroyed, so the release count is minused by one, should print that the "obj" is released
|
|
|
|
|
|
|
|
{
|
2014-01-21 17:47:59 +08:00
|
|
|
AutoreleasePool pool2;
|
2014-01-22 10:17:42 +08:00
|
|
|
for (int i = 0; i < 100; ++i)
|
2014-01-21 17:47:59 +08:00
|
|
|
{
|
|
|
|
TestObject *tmpObj = new TestObject();
|
|
|
|
tmpObj->autorelease();
|
|
|
|
}
|
|
|
|
pool2.dump();
|
|
|
|
}
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(this);
|
|
|
|
}
|