1
我正在cocos2dx v3.3上工作。無論何時使用Release或autorelease發佈圖像,然後在Android中發生隨機崩潰,但它在iOS中工作得很好。崩潰在參考::發佈()在android cocos2d-x
任何人都可以幫助或指導我如何處理這些崩潰?
我正在cocos2dx v3.3上工作。無論何時使用Release或autorelease發佈圖像,然後在Android中發生隨機崩潰,但它在iOS中工作得很好。崩潰在參考::發佈()在android cocos2d-x
任何人都可以幫助或指導我如何處理這些崩潰?
cocos2d-x爲存儲器管理實現了類似OC的引用計數方案。 這種崩潰通常是由錯誤的引用計數造成的。當你release
或autorelease
a Ref
,你應該確保之前已經編輯了retain
,以便其引用計數不爲零。
進一步的細節,可以參考下面的代碼片段從CCRef.cpp
(注:評論是更重要):
void Ref::release()
{
CCASSERT(_referenceCount > 0, "reference count should be greater than 0");
--_referenceCount;
if (_referenceCount == 0)
{
#if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
auto poolManager = PoolManager::getInstance();
if (!poolManager->getCurrentPool()->isClearing() && poolManager->isObjectInPools(this))
{
// Trigger an assert if the reference count is 0 but the Ref is still in autorelease pool.
// This happens when 'autorelease/release' were not used in pairs with 'new/retain'.
//
// Wrong usage (1):
//
// auto obj = Node::create(); // Ref = 1, but it's an autorelease Ref which means it was in the autorelease pool.
// obj->autorelease(); // Wrong: If you wish to invoke autorelease several times, you should retain `obj` first.
//
// Wrong usage (2):
//
// auto obj = Node::create();
// obj->release(); // Wrong: obj is an autorelease Ref, it will be released when clearing current pool.
//
// 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.
//
// Correct usage (2):
//
// 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.");
}
#endif
#if CC_REF_LEAK_DETECTION
untrackRef(this);
#endif
delete this;
}
}