From 9ace0d2fdc8d0b48290f212614cb8ef760f0d462 Mon Sep 17 00:00:00 2001 From: James Chen Date: Mon, 16 Apr 2012 10:57:37 +0800 Subject: [PATCH] Fixed memory leaks in CCString and CCDictionary, Updated CCFileUtils_ios.mm.Remove inline prefix for some member function of CCString, so that make it compiled successfully on android and ios. --- .../project.pbxproj.REMOVED.git-id | 2 +- cocos2dx/cocoa/CCDictionary.cpp | 4 +- cocos2dx/cocoa/CCString.cpp | 5 ++- cocos2dx/include/CCString.h | 41 ++++++++++++------- cocos2dx/platform/ios/CCFileUtils_ios.mm | 15 ++----- .../project.pbxproj.REMOVED.git-id | 2 +- tests/tests/controller.cpp | 3 ++ 7 files changed, 43 insertions(+), 29 deletions(-) diff --git a/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id b/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id index 8894e9c69d..9f3249cea0 100644 --- a/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -2d9494d763406ba9945e40f011820005d14b92eb \ No newline at end of file +ba02d3744870cd69d88ef40bfe303f99d85313a6 \ No newline at end of file diff --git a/cocos2dx/cocoa/CCDictionary.cpp b/cocos2dx/cocoa/CCDictionary.cpp index e059ef81df..004088bc13 100644 --- a/cocos2dx/cocoa/CCDictionary.cpp +++ b/cocos2dx/cocoa/CCDictionary.cpp @@ -293,7 +293,9 @@ CCDictionary* CCDictionary::dictionaryWithContentsOfFileThreadSafe(const char *p CCDictionary* CCDictionary::dictionaryWithContentsOfFile(const char *pFileName) { - return dictionaryWithContentsOfFileThreadSafe(pFileName); + CCDictionary* pRet = dictionaryWithContentsOfFileThreadSafe(pFileName); + pRet->autorelease(); + return pRet; } NS_CC_END diff --git a/cocos2dx/cocoa/CCString.cpp b/cocos2dx/cocoa/CCString.cpp index 75f0bb74ef..17d800b3e6 100644 --- a/cocos2dx/cocoa/CCString.cpp +++ b/cocos2dx/cocoa/CCString.cpp @@ -180,8 +180,11 @@ CCString* CCString::stringWithContentsOfFile(const char* pszFileName) { unsigned long size = 0; unsigned char* pData = 0; + CCString* pRet = NULL; pData = CCFileUtils::getFileData(pszFileName, "rb", &size); - return stringWithData(pData, size); + pRet = stringWithData(pData, size); + CC_SAFE_DELETE_ARRAY(pData); + return pRet; } NS_CC_END diff --git a/cocos2dx/include/CCString.h b/cocos2dx/include/CCString.h index e3178fdb83..3cf931cfd6 100755 --- a/cocos2dx/include/CCString.h +++ b/cocos2dx/include/CCString.h @@ -39,48 +39,61 @@ public: virtual ~CCString(); - /** override assignment operator */ + /* override assignment operator */ CCString& operator= (const CCString& other); /** init a string with format, it's similar with the c function 'sprintf' */ bool initWithFormat(const char* format, ...); /** convert to int value */ - inline int intValue() const; + int intValue() const; /** convert to unsigned int value */ - inline unsigned int uintValue() const; + unsigned int uintValue() const; /** convert to float value */ - inline float floatValue() const; + float floatValue() const; /** convert to double value */ - inline double doubleValue() const; + double doubleValue() const; /** convert to bool value */ - inline bool boolValue() const; + bool boolValue() const; /** get the C string */ - inline const char* getCString() const; + const char* getCString() const; /** get the length of string */ - inline unsigned int length() const; + unsigned int length() const; - /** override functions */ + /* override functions */ virtual CCObject* copyWithZone(CCZone* pZone); virtual bool isEqual(const CCObject* pObject); - /** static funcitons */ - /** create a string with c string */ + /* static funcitons */ + /** create a string with c string + * @return A CCString pointer which is an autorelease object pointer, + * it means that you needn't do a release operation unless you retain it. + */ static CCString* stringWithCString(const char* pStr); - /** create a string with format, it's similar with the c function 'sprintf' */ + /** create a string with format, it's similar with the c function 'sprintf', the default buffer size is (1024*100) bytes, + * if you want to change it, you should modify the kMaxStringLen macro in CCString.cpp file. + * @return A CCString pointer which is an autorelease object pointer, + * it means that you needn't do a release operation unless you retain it. + */ static CCString* stringWithFormat(const char* format, ...); - /** create a string with binary data */ + /** create a string with binary data + * @return A CCString pointer which is an autorelease object pointer, + * it means that you needn't do a release operation unless you retain it. + */ static CCString* stringWithData(unsigned char* pData, unsigned long nLen); - /** create a string with a file */ + /** create a string with a file, + * @return A CCString pointer which is an autorelease object pointer, + * it means that you needn't do a release operation unless you retain it. + */ static CCString* stringWithContentsOfFile(const char* pszFileName); private: diff --git a/cocos2dx/platform/ios/CCFileUtils_ios.mm b/cocos2dx/platform/ios/CCFileUtils_ios.mm index 509e806589..f6af5df46c 100644 --- a/cocos2dx/platform/ios/CCFileUtils_ios.mm +++ b/cocos2dx/platform/ios/CCFileUtils_ios.mm @@ -34,6 +34,7 @@ THE SOFTWARE. #include "CCFileUtils.h" #include "CCDirector.h" #include "CCSAXParser.h" +#include "CCDictionary.h" #include "support/zip_support/unzip.h" #define MAX_PATH 260 @@ -415,17 +416,9 @@ namespace cocos2d { return pRet->m_sString.c_str(); } - CCDictionary *CCFileUtils::dictionaryWithContentsOfFile(const char *pFileName) + CCDictionary* ccFileUtils_dictionaryWithContentsOfFileThreadSafe(const char *pFileName) { - CCDictionary *ret = dictionaryWithContentsOfFileThreadSafe(pFileName); - ret->autorelease(); - - return ret; - } - - CCDictionary *CCFileUtils::dictionaryWithContentsOfFileThreadSafe(const char *pFileName) - { - const char* pszFullPath = fullPathFromRelativePath(pFileName); + const char* pszFullPath = CCFileUtils::fullPathFromRelativePath(pFileName); NSString* pPath = [NSString stringWithUTF8String:pszFullPath]; NSDictionary* pDict = [NSDictionary dictionaryWithContentsOfFile:pPath]; @@ -434,7 +427,7 @@ namespace cocos2d { id value = [pDict objectForKey:key]; static_addValueToCCDict(key, value, pRet); } - + return pRet; } diff --git a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id index 205c3a737f..61c40a8718 100644 --- a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -80c6fdfc6a77f7b0475ad0c3edbc6097d604d506 \ No newline at end of file +b9cda1260ba870cf512eba65876376a4b0f6c7df \ No newline at end of file diff --git a/tests/tests/controller.cpp b/tests/tests/controller.cpp index 406421b56a..f618765985 100644 --- a/tests/tests/controller.cpp +++ b/tests/tests/controller.cpp @@ -187,6 +187,9 @@ void TestController::menuCallback(CCObject * pSender) void TestController::closeCallback(CCObject * pSender) { CCDirector::sharedDirector()->end(); +#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) + exit(0); +#endif } void TestController::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)