PuzzleSDK
ReleasePoolTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "ReleasePoolTest.h"
26
27using namespace cocos2d;
28
29ReleasePoolTests::ReleasePoolTests()
30{
32}
33
34class TestObject : public Ref
35{
36public:
37 TestObject() : _name(""){}
38
39 TestObject(std::string name) : _name(name)
40 {
41 CCLOG("TestObject:%s is created", _name.c_str());
42 }
43
45 {
46 if (_name.size() > 0)
47 CCLOG("TestObject:%s is destroyed", _name.c_str());
48 }
49
50private:
51 std::string _name;
52};
53
55{
56 if (!TestCase::init())
57 {
58 return false;
59 }
60 // title
61 auto label = Label::createWithTTF("AutoreasePool Test", "fonts/arial.ttf", 32);
62 addChild(label, 9999);
63 label->setPosition(Vec2(VisibleRect::center().x, VisibleRect::top().y - 30));
64
65 // reference count should be added when added into auto release pool
66
67 TestObject *obj = new (std::nothrow) TestObject("testobj");
68 obj->autorelease();
69 assert(obj->getReferenceCount() == 1);
70
71 // should retain first before invoking autorelease
72 obj->retain();
73 obj->autorelease();
74 assert(obj->getReferenceCount() == 2);
75
76 // create an autorelease pool in stack
77
78 {
79 AutoreleasePool pool1;
80
81 // can invoke autorelease more than once
82 obj->retain();
83 obj->autorelease();
84 assert(obj->getReferenceCount() == 3);
85 obj->retain();
86 obj->autorelease();
87 assert(obj->getReferenceCount() == 4);
88
89 // retain, release can work together with autorelease pool
90 obj->retain();
91 assert(obj->getReferenceCount() == 5);
92 obj->release();
93 assert(obj->getReferenceCount() == 4);
94 }
95
96 assert(obj->getReferenceCount() == 2);
97
98 // example of using temple autorelease pool
99 {
100 AutoreleasePool pool2;
101 char name[20];
102 for (int i = 0; i < 100; ++i)
103 {
104 snprintf(name, 20, "object%d", i);
105 TestObject *tmpObj = new (std::nothrow) TestObject(name);
106 tmpObj->autorelease();
107 }
108 }
109
110 // object in pool2 should be released
111
112 {
113 new AutoreleasePool;
114 PoolManager::destroyInstance();
115 }
116
117 return true;
118}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
virtual bool init() override
std::string _name
TestObject(std::string name)
static cocos2d::Vec2 top()
Definition: VisibleRect.cpp:57
static cocos2d::Vec2 center()
Definition: VisibleRect.cpp:69