2014-01-21 17:47:59 +08:00
|
|
|
#include "ReleasePoolTest.h"
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
ReleasePoolTests::ReleasePoolTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(ReleasePoolTest);
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
class TestObject : public Ref
|
2014-01-21 17:47:59 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
bool ReleasePoolTest::init()
|
2014-01-21 17:47:59 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
if (!TestCase::init())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-21 17:47:59 +08:00
|
|
|
// title
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF("AutoreasePool Test", "fonts/arial.ttf", 32);
|
2014-01-21 17:47:59 +08:00
|
|
|
addChild(label, 9999);
|
2014-05-15 01:07:09 +08:00
|
|
|
label->setPosition(Vec2(VisibleRect::center().x, VisibleRect::top().y - 30));
|
2014-01-21 17:47:59 +08:00
|
|
|
|
|
|
|
// reference count should be added when added into auto release pool
|
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
TestObject *obj = new (std::nothrow) TestObject("testobj");
|
2014-01-21 17:47:59 +08:00
|
|
|
obj->autorelease();
|
2014-01-22 13:47:29 +08:00
|
|
|
assert(obj->getReferenceCount() == 1);
|
2014-01-21 17:47:59 +08:00
|
|
|
|
2014-01-22 11:40:07 +08:00
|
|
|
// should retain first before invoking autorelease
|
|
|
|
obj->retain();
|
2014-01-21 17:47:59 +08:00
|
|
|
obj->autorelease();
|
2014-01-22 13:47:29 +08:00
|
|
|
assert(obj->getReferenceCount() == 2);
|
2014-01-21 17:47:59 +08:00
|
|
|
|
|
|
|
// create an autorelease pool in stack
|
|
|
|
|
|
|
|
{
|
|
|
|
AutoreleasePool pool1;
|
|
|
|
|
2014-01-22 11:40:07 +08:00
|
|
|
// can invoke autorelease more than once
|
|
|
|
obj->retain();
|
2014-01-21 17:47:59 +08:00
|
|
|
obj->autorelease();
|
2014-01-22 13:47:29 +08:00
|
|
|
assert(obj->getReferenceCount() == 3);
|
2014-01-22 11:40:07 +08:00
|
|
|
obj->retain();
|
|
|
|
obj->autorelease();
|
2014-01-22 13:47:29 +08:00
|
|
|
assert(obj->getReferenceCount() == 4);
|
2014-01-21 17:47:59 +08:00
|
|
|
|
|
|
|
// retain, release can work together with autorelease pool
|
|
|
|
obj->retain();
|
2014-01-22 13:47:29 +08:00
|
|
|
assert(obj->getReferenceCount() == 5);
|
2014-01-21 17:47:59 +08:00
|
|
|
obj->release();
|
2014-01-22 13:47:29 +08:00
|
|
|
assert(obj->getReferenceCount() == 4);
|
2014-01-22 10:17:42 +08:00
|
|
|
}
|
|
|
|
|
2014-01-22 13:47:29 +08:00
|
|
|
assert(obj->getReferenceCount() == 2);
|
2014-01-22 10:17:42 +08:00
|
|
|
|
2014-01-22 11:40:07 +08:00
|
|
|
// example of using temple autorelease pool
|
2014-01-22 10:17:42 +08:00
|
|
|
{
|
2014-01-21 17:47:59 +08:00
|
|
|
AutoreleasePool pool2;
|
2014-01-22 11:40:07 +08:00
|
|
|
char name[20];
|
2014-01-22 10:17:42 +08:00
|
|
|
for (int i = 0; i < 100; ++i)
|
2014-01-21 17:47:59 +08:00
|
|
|
{
|
2014-01-22 11:40:07 +08:00
|
|
|
snprintf(name, 20, "object%d", i);
|
2014-08-28 07:31:57 +08:00
|
|
|
TestObject *tmpObj = new (std::nothrow) TestObject(name);
|
2014-01-21 17:47:59 +08:00
|
|
|
tmpObj->autorelease();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 11:40:07 +08:00
|
|
|
// object in pool2 should be released
|
2014-06-03 17:58:09 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
new AutoreleasePool;
|
|
|
|
PoolManager::destroyInstance();
|
|
|
|
}
|
2014-01-22 11:40:07 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
return true;
|
2014-01-21 17:47:59 +08:00
|
|
|
}
|