From 2c590cea3efbdf75bf5dc76cbaae57cb9accfb62 Mon Sep 17 00:00:00 2001 From: James Chen Date: Fri, 20 Dec 2013 14:06:24 +0800 Subject: [PATCH] closed #3457: Fix of UserDefault. Infinite loop in UserDefault's destructor on iOS. The destructor of UserDefault is private now. --- cocos/2d/CCUserDefault.cpp | 11 ++--------- cocos/2d/CCUserDefault.h | 8 ++------ cocos/2d/CCUserDefault.mm | 12 ++---------- cocos/2d/CCUserDefaultAndroid.cpp | 11 ++--------- 4 files changed, 8 insertions(+), 34 deletions(-) diff --git a/cocos/2d/CCUserDefault.cpp b/cocos/2d/CCUserDefault.cpp index 4e876baefd..a423f449d0 100644 --- a/cocos/2d/CCUserDefault.cpp +++ b/cocos/2d/CCUserDefault.cpp @@ -140,23 +140,16 @@ static void setValueForKey(const char* pKey, const char* pValue) * implements of UserDefault */ -UserDefault* UserDefault::_userDefault = 0; +UserDefault* UserDefault::_userDefault = nullptr; string UserDefault::_filePath = string(""); bool UserDefault::_isFilePathInitialized = false; -/** - * If the user invoke delete UserDefault::getInstance(), should set _userDefault - * to null to avoid error when he invoke UserDefault::getInstance() later. - */ UserDefault::~UserDefault() { - CC_SAFE_DELETE(_userDefault); - _userDefault = nullptr; } UserDefault::UserDefault() { - _userDefault = nullptr; } bool UserDefault::getBoolForKey(const char* pKey) @@ -433,7 +426,7 @@ UserDefault* UserDefault::getInstance() void UserDefault::destroyInstance() { - _userDefault = nullptr; + CC_SAFE_DELETE(_userDefault); } // XXX: deprecated diff --git a/cocos/2d/CCUserDefault.h b/cocos/2d/CCUserDefault.h index a18fb00b6a..e14a616e37 100644 --- a/cocos/2d/CCUserDefault.h +++ b/cocos/2d/CCUserDefault.h @@ -46,12 +46,6 @@ NS_CC_BEGIN class CC_DLL UserDefault { public: - /** - * @js NA - * @lua NA - */ - ~UserDefault(); - // get value methods /** @@ -186,6 +180,8 @@ public: private: UserDefault(); + ~UserDefault(); + static bool createXMLFile(); static void initXMLFilePath(); diff --git a/cocos/2d/CCUserDefault.mm b/cocos/2d/CCUserDefault.mm index 8bbe0792d8..c2577d8dc7 100644 --- a/cocos/2d/CCUserDefault.mm +++ b/cocos/2d/CCUserDefault.mm @@ -48,7 +48,7 @@ NS_CC_BEGIN * implements of UserDefault */ -UserDefault* UserDefault::_userDefault = 0; +UserDefault* UserDefault::_userDefault = nullptr; string UserDefault::_filePath = string(""); bool UserDefault::_isFilePathInitialized = false; @@ -134,19 +134,12 @@ static void deleteNodeByKey(const char *pKey) } #endif -/** - * If the user invoke delete UserDefault::getInstance(), should set _userDefault - * to null to avoid error when he invoke UserDefault::getInstance() later. - */ UserDefault::~UserDefault() { - CC_SAFE_DELETE(_userDefault); - _userDefault = nullptr; } UserDefault::UserDefault() { - _userDefault = nullptr; } bool UserDefault::getBoolForKey(const char* pKey) @@ -503,8 +496,7 @@ UserDefault* UserDefault::getInstance() void UserDefault::destroyInstance() { - delete _userDefault; - _userDefault = nullptr; + CC_SAFE_DELETE(_userDefault); } // XXX: deprecated diff --git a/cocos/2d/CCUserDefaultAndroid.cpp b/cocos/2d/CCUserDefaultAndroid.cpp index c72718eef8..cfa900125f 100644 --- a/cocos/2d/CCUserDefaultAndroid.cpp +++ b/cocos/2d/CCUserDefaultAndroid.cpp @@ -49,7 +49,7 @@ NS_CC_BEGIN * implements of UserDefault */ -UserDefault* UserDefault::_userDefault = 0; +UserDefault* UserDefault::_userDefault = nullptr; string UserDefault::_filePath = string(""); bool UserDefault::_isFilePathInitialized = false; @@ -135,19 +135,12 @@ static void deleteNodeByKey(const char *pKey) } #endif -/** - * If the user invoke delete UserDefault::getInstance(), should set _userDefault - * to null to avoid error when he invoke UserDefault::getInstance() later. - */ UserDefault::~UserDefault() { - CC_SAFE_DELETE(_userDefault); - _userDefault = nullptr; } UserDefault::UserDefault() { - _userDefault = nullptr; } // XXX: deprecated @@ -158,7 +151,7 @@ void UserDefault::purgeSharedUserDefault() void UserDefault::destroyInstance() { - _userDefault = nullptr; + CC_SAFE_DELETE(_userDefault); } bool UserDefault::getBoolForKey(const char* pKey)