diff --git a/cocos2dx/particle_nodes/CCParticleSystem.cpp b/cocos2dx/particle_nodes/CCParticleSystem.cpp index df7fbeb223..bccbc0bf7b 100644 --- a/cocos2dx/particle_nodes/CCParticleSystem.cpp +++ b/cocos2dx/particle_nodes/CCParticleSystem.cpp @@ -141,11 +141,15 @@ CCParticleSystem * CCParticleSystem::particleWithFile(const char *plistFile) } bool CCParticleSystem::initWithFile(const char *plistFile) { + bool bRet = false; m_sPlistFile = CCFileUtils::fullPathFromRelativePath(plistFile); - CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFile(m_sPlistFile.c_str()); + CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(m_sPlistFile.c_str()); CCAssert( dict != NULL, "Particles: file not found"); - return this->initWithDictionary(dict); + bRet = this->initWithDictionary(dict); + dict->release(); + + return bRet; } bool CCParticleSystem::initWithDictionary(CCDictionary *dictionary) diff --git a/cocos2dx/platform/CCFileUtils.cpp b/cocos2dx/platform/CCFileUtils.cpp index d121215ea6..c17ec0ee16 100644 --- a/cocos2dx/platform/CCFileUtils.cpp +++ b/cocos2dx/platform/CCFileUtils.cpp @@ -98,7 +98,9 @@ public: m_pCurDict = new CCDictionary(); if(! m_pRootDict) { + // Because it will call m_pCurDict->release() later, so retain here. m_pRootDict = m_pCurDict; + m_pRootDict->retain(); } m_tState = SAX_DICT; @@ -120,7 +122,8 @@ public: CCDictionary* pPreDict = m_tDictStack.top(); pPreDict->setObject(m_pCurDict, m_sCurKey); } - m_pCurDict->autorelease(); + + m_pCurDict->release(); // record the dict state m_tStateStack.push(m_tState); @@ -285,7 +288,15 @@ std::string& CCFileUtils::ccRemoveHDSuffixFromFile(std::string& path) CCDictionary *CCFileUtils::dictionaryWithContentsOfFile(const char *pFileName) { - CCDictMaker tMaker; + CCDictionary *ret = dictionaryWithContentsOfFileThreadSafe(pFileName); + ret->autorelease(); + + return ret; +} + +CCDictionary *CCFileUtils::dictionaryWithContentsOfFileThreadSafe(const char *pFileName) +{ + CCDictMaker tMaker; return tMaker.dictionaryWithContentsOfFile(pFileName); } diff --git a/cocos2dx/platform/CCFileUtils.h b/cocos2dx/platform/CCFileUtils.h index 84334b7e25..093dd54eba 100644 --- a/cocos2dx/platform/CCFileUtils.h +++ b/cocos2dx/platform/CCFileUtils.h @@ -89,6 +89,12 @@ public: */ static CCDictionary *dictionaryWithContentsOfFile(const char *pFileName); + /* + @brief The same meaning as dictionaryWithContentsOfFile(), but it doesn't call autorelease, so the + invoker should call release(). + */ + static CCDictionary *dictionaryWithContentsOfFileThreadSafe(const char *pFileName); + /** @brief Get the writeable path @return The path that can write/read file diff --git a/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp b/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp index 877497b060..b7eae23b38 100644 --- a/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp +++ b/cocos2dx/sprite_nodes/CCSpriteFrameCache.cpp @@ -197,9 +197,11 @@ void CCSpriteFrameCache::addSpriteFramesWithDictionary(CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFile(pszPath); + CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(pszPath); addSpriteFramesWithDictionary(dict, pobTexture); + + dict->release(); } void CCSpriteFrameCache::addSpriteFramesWithFile(const char* plist, const char* textureFileName) @@ -220,7 +222,7 @@ void CCSpriteFrameCache::addSpriteFramesWithFile(const char* plist, const char* void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist) { const char *pszPath = CCFileUtils::fullPathFromRelativePath(pszPlist); - CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFile(pszPath); + CCDictionary *dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(pszPath); string texturePath(""); @@ -261,6 +263,8 @@ void CCSpriteFrameCache::addSpriteFramesWithFile(const char *pszPlist) { CCLOG("cocos2d: CCSpriteFrameCache: Couldn't load texture"); } + + dict->release(); } void CCSpriteFrameCache::addSpriteFrame(CCSpriteFrame *pobFrame, const char *pszFrameName) @@ -316,9 +320,11 @@ void CCSpriteFrameCache::removeSpriteFrameByName(const char *pszName) void CCSpriteFrameCache::removeSpriteFramesFromFile(const char* plist) { const char* path = CCFileUtils::fullPathFromRelativePath(plist); - CCDictionary* dict = CCFileUtils::dictionaryWithContentsOfFile(path); + CCDictionary* dict = CCFileUtils::dictionaryWithContentsOfFileThreadSafe(path); removeSpriteFramesFromDictionary((CCDictionary*)dict); + + dict->release(); } void CCSpriteFrameCache::removeSpriteFramesFromDictionary(CCDictionary *dictionary)