2013-10-12 15:25:45 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:47:11 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2017-02-14 14:36:57 +08:00
|
|
|
Copyright (c) 2013-2017 Chukong Technologies
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2013-10-12 15:25:45 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "base/CCAutoreleasePool.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCScriptSupport.h"
|
2013-10-12 15:25:45 +08:00
|
|
|
|
2014-09-26 10:44:29 +08:00
|
|
|
#if CC_REF_LEAK_DETECTION
|
2014-05-14 10:31:09 +08:00
|
|
|
#include <algorithm> // std::find
|
2016-08-30 09:36:45 +08:00
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
2014-05-14 10:31:09 +08:00
|
|
|
#endif
|
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-09-26 10:44:29 +08:00
|
|
|
#if CC_REF_LEAK_DETECTION
|
2014-05-14 11:55:20 +08:00
|
|
|
static void trackRef(Ref* ref);
|
|
|
|
static void untrackRef(Ref* ref);
|
2014-05-14 10:07:03 +08:00
|
|
|
#endif
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref::Ref()
|
|
|
|
: _referenceCount(1) // when the Ref is created, the reference count of it is 1
|
2015-12-03 06:45:13 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
, _luaID (0)
|
|
|
|
, _scriptObject(nullptr)
|
|
|
|
, _rooted(false)
|
|
|
|
#endif
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-02-20 16:40:46 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-10-12 15:25:45 +08:00
|
|
|
static unsigned int uObjectCount = 0;
|
|
|
|
_ID = ++uObjectCount;
|
2014-02-20 16:40:46 +08:00
|
|
|
#endif
|
2014-05-14 10:31:09 +08:00
|
|
|
|
2014-09-26 10:44:29 +08:00
|
|
|
#if CC_REF_LEAK_DETECTION
|
2014-05-14 10:07:03 +08:00
|
|
|
trackRef(this);
|
2014-05-14 10:31:09 +08:00
|
|
|
#endif
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref::~Ref()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2016-02-21 20:20:32 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
2013-10-12 15:25:45 +08:00
|
|
|
// if the object is referenced by Lua engine, remove it
|
|
|
|
if (_luaID)
|
|
|
|
{
|
|
|
|
ScriptEngineManager::getInstance()->getScriptEngine()->removeScriptObjectByObject(this);
|
|
|
|
}
|
2016-02-21 20:20:32 +08:00
|
|
|
#if !CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
2013-10-12 15:25:45 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ScriptEngineProtocol* pEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
2014-07-10 00:45:27 +08:00
|
|
|
if (pEngine != nullptr && pEngine->getScriptType() == kScriptTypeJavascript)
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
|
|
|
pEngine->removeScriptObjectByObject(this);
|
|
|
|
}
|
|
|
|
}
|
2016-02-21 20:20:32 +08:00
|
|
|
#endif // !CC_ENABLE_GC_FOR_NATIVE_OBJECTS
|
|
|
|
#endif // CC_ENABLE_SCRIPT_BINDING
|
2014-05-14 10:07:03 +08:00
|
|
|
|
2014-05-14 10:25:45 +08:00
|
|
|
|
2014-09-26 10:44:29 +08:00
|
|
|
#if CC_REF_LEAK_DETECTION
|
2014-05-14 10:07:03 +08:00
|
|
|
if (_referenceCount != 0)
|
|
|
|
untrackRef(this);
|
|
|
|
#endif
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void Ref::retain()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2015-01-20 15:32:18 +08:00
|
|
|
CCASSERT(_referenceCount > 0, "reference count should be greater than 0");
|
2014-02-20 10:53:49 +08:00
|
|
|
++_referenceCount;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void Ref::release()
|
2014-01-24 14:46:06 +08:00
|
|
|
{
|
2015-01-20 15:32:18 +08:00
|
|
|
CCASSERT(_referenceCount > 0, "reference count should be greater than 0");
|
2014-01-24 14:46:06 +08:00
|
|
|
--_referenceCount;
|
2014-05-14 10:25:45 +08:00
|
|
|
|
2014-01-24 14:46:06 +08:00
|
|
|
if (_referenceCount == 0)
|
|
|
|
{
|
2014-01-26 18:34:48 +08:00
|
|
|
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
|
2014-01-24 17:51:36 +08:00
|
|
|
auto poolManager = PoolManager::getInstance();
|
|
|
|
if (!poolManager->getCurrentPool()->isClearing() && poolManager->isObjectInPools(this))
|
2014-01-24 14:46:06 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
// Trigger an assert if the reference count is 0 but the Ref is still in autorelease pool.
|
2014-01-24 14:46:06 +08:00
|
|
|
// This happens when 'autorelease/release' were not used in pairs with 'new/retain'.
|
|
|
|
//
|
|
|
|
// Wrong usage (1):
|
|
|
|
//
|
2014-02-20 10:53:49 +08:00
|
|
|
// auto obj = Node::create(); // Ref = 1, but it's an autorelease Ref which means it was in the autorelease pool.
|
2014-01-24 14:46:06 +08:00
|
|
|
// obj->autorelease(); // Wrong: If you wish to invoke autorelease several times, you should retain `obj` first.
|
|
|
|
//
|
|
|
|
// Wrong usage (2):
|
|
|
|
//
|
|
|
|
// auto obj = Node::create();
|
2014-02-20 10:53:49 +08:00
|
|
|
// obj->release(); // Wrong: obj is an autorelease Ref, it will be released when clearing current pool.
|
2014-01-24 14:46:06 +08:00
|
|
|
//
|
|
|
|
// Correct usage (1):
|
|
|
|
//
|
|
|
|
// auto obj = Node::create();
|
|
|
|
// |- new Node(); // `new` is the pair of the `autorelease` of next line
|
|
|
|
// |- autorelease(); // The pair of `new Node`.
|
|
|
|
//
|
|
|
|
// obj->retain();
|
|
|
|
// obj->autorelease(); // This `autorelease` is the pair of `retain` of previous line.
|
|
|
|
//
|
2014-01-24 22:34:14 +08:00
|
|
|
// Correct usage (2):
|
2014-01-24 14:46:06 +08:00
|
|
|
//
|
|
|
|
// auto obj = Node::create();
|
|
|
|
// obj->retain();
|
|
|
|
// obj->release(); // This `release` is the pair of `retain` of previous line.
|
|
|
|
CCASSERT(false, "The reference shouldn't be 0 because it is still in autorelease pool.");
|
|
|
|
}
|
2014-01-26 18:34:48 +08:00
|
|
|
#endif
|
2014-05-14 10:25:45 +08:00
|
|
|
|
2017-03-16 09:42:16 +08:00
|
|
|
#if CC_ENABLE_SCRIPT_BINDING
|
|
|
|
ScriptEngineProtocol* pEngine = ScriptEngineManager::getInstance()->getScriptEngine();
|
|
|
|
if (pEngine != nullptr && pEngine->getScriptType() == kScriptTypeJavascript)
|
|
|
|
{
|
|
|
|
pEngine->removeObjectProxy(this);
|
|
|
|
}
|
|
|
|
#endif // CC_ENABLE_SCRIPT_BINDING
|
|
|
|
|
2014-09-26 10:44:29 +08:00
|
|
|
#if CC_REF_LEAK_DETECTION
|
2014-05-14 10:07:03 +08:00
|
|
|
untrackRef(this);
|
|
|
|
#endif
|
2014-01-24 14:46:06 +08:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* Ref::autorelease()
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-02-20 10:53:49 +08:00
|
|
|
PoolManager::getInstance()->getCurrentPool()->addObject(this);
|
|
|
|
return this;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
unsigned int Ref::getReferenceCount() const
|
2013-10-12 15:25:45 +08:00
|
|
|
{
|
2014-01-22 13:47:29 +08:00
|
|
|
return _referenceCount;
|
2013-10-12 15:25:45 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 10:44:29 +08:00
|
|
|
#if CC_REF_LEAK_DETECTION
|
2014-05-14 10:07:03 +08:00
|
|
|
|
2016-08-30 09:36:45 +08:00
|
|
|
static std::vector<Ref*> __refAllocationList;
|
|
|
|
static std::mutex __refMutex;
|
2014-05-14 10:07:03 +08:00
|
|
|
|
|
|
|
void Ref::printLeaks()
|
|
|
|
{
|
2016-08-30 09:36:45 +08:00
|
|
|
std::lock_guard<std::mutex> refLockGuard(__refMutex);
|
2014-05-14 10:07:03 +08:00
|
|
|
// Dump Ref object memory leaks
|
|
|
|
if (__refAllocationList.empty())
|
|
|
|
{
|
|
|
|
log("[memory] All Ref objects successfully cleaned up (no leaks detected).\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
log("[memory] WARNING: %d Ref objects still active in memory.\n", (int)__refAllocationList.size());
|
2014-05-14 10:25:45 +08:00
|
|
|
|
2014-05-14 10:07:03 +08:00
|
|
|
for (const auto& ref : __refAllocationList)
|
|
|
|
{
|
|
|
|
CC_ASSERT(ref);
|
|
|
|
const char* type = typeid(*ref).name();
|
|
|
|
log("[memory] LEAK: Ref object '%s' still active with reference count %d.\n", (type ? type : ""), ref->getReferenceCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 11:55:20 +08:00
|
|
|
static void trackRef(Ref* ref)
|
2014-05-14 10:07:03 +08:00
|
|
|
{
|
2016-08-30 09:36:45 +08:00
|
|
|
std::lock_guard<std::mutex> refLockGuard(__refMutex);
|
2014-05-14 11:55:20 +08:00
|
|
|
CCASSERT(ref, "Invalid parameter, ref should not be null!");
|
2014-05-14 10:25:45 +08:00
|
|
|
|
2014-05-14 10:07:03 +08:00
|
|
|
// Create memory allocation record.
|
|
|
|
__refAllocationList.push_back(ref);
|
|
|
|
}
|
|
|
|
|
2014-05-14 11:55:20 +08:00
|
|
|
static void untrackRef(Ref* ref)
|
2014-05-14 10:07:03 +08:00
|
|
|
{
|
2016-08-30 09:36:45 +08:00
|
|
|
std::lock_guard<std::mutex> refLockGuard(__refMutex);
|
2014-05-14 10:07:03 +08:00
|
|
|
auto iter = std::find(__refAllocationList.begin(), __refAllocationList.end(), ref);
|
|
|
|
if (iter == __refAllocationList.end())
|
|
|
|
{
|
|
|
|
log("[memory] CORRUPTION: Attempting to free (%s) with invalid ref tracking record.\n", typeid(*ref).name());
|
|
|
|
return;
|
|
|
|
}
|
2014-05-14 10:25:45 +08:00
|
|
|
|
2014-05-14 10:07:03 +08:00
|
|
|
__refAllocationList.erase(iter);
|
|
|
|
}
|
|
|
|
|
2014-11-05 15:23:25 +08:00
|
|
|
#endif // #if CC_REF_LEAK_DETECTION
|
2014-05-14 10:07:03 +08:00
|
|
|
|
|
|
|
|
2013-10-12 15:25:45 +08:00
|
|
|
NS_CC_END
|