From 905443d17ce58e4fc1eac39be8c9c19d2476d2f0 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 18 Feb 2014 10:11:04 +0800 Subject: [PATCH 1/4] new[] free mismatching after PR #5326. --- cocos/2d/platform/android/CCDevice.cpp | 7 ++----- cocos/2d/platform/ios/CCDevice.mm | 4 ++-- cocos/2d/platform/linux/CCDevice.cpp | 2 +- cocos/2d/platform/mac/CCDevice.mm | 2 +- cocos/2d/platform/win32/CCDevice.cpp | 2 +- 5 files changed, 7 insertions(+), 10 deletions(-) diff --git a/cocos/2d/platform/android/CCDevice.cpp b/cocos/2d/platform/android/CCDevice.cpp index f30790afb7..d3136a41a3 100644 --- a/cocos/2d/platform/android/CCDevice.cpp +++ b/cocos/2d/platform/android/CCDevice.cpp @@ -78,10 +78,7 @@ public: ~BitmapDC(void) { - if (_data) - { - delete [] _data; - } + CC_SAFE_FREE(_data); } bool getBitmapFromJavaShadowStroke( const char *text, @@ -222,7 +219,7 @@ extern "C" cocos2d::BitmapDC& bitmapDC = cocos2d::sharedBitmapDC(); bitmapDC._width = width; bitmapDC._height = height; - bitmapDC._data = new unsigned char[size]; + bitmapDC._data = (unsigned char*)malloc(sizeof(unsigned char) * size); env->GetByteArrayRegion(pixels, 0, size, (jbyte*)bitmapDC._data); } }; diff --git a/cocos/2d/platform/ios/CCDevice.mm b/cocos/2d/platform/ios/CCDevice.mm index ba6bfd76cf..7d91cb68ce 100644 --- a/cocos/2d/platform/ios/CCDevice.mm +++ b/cocos/2d/platform/ios/CCDevice.mm @@ -326,7 +326,7 @@ static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, dim.height += shadowStrokePaddingY; - unsigned char* data = new unsigned char[(int)(dim.width * dim.height * 4)]; + unsigned char* data = (unsigned char*)malloc(sizeof(unsigned char) * (int)(dim.width * dim.height * 4)); memset(data, 0, (int)(dim.width * dim.height * 4)); // draw text @@ -341,7 +341,7 @@ static bool _initWithString(const char * text, cocos2d::Device::TextAlign align, if (!context) { CGColorSpaceRelease(colorSpace); - delete[] data; + CC_SAFE_FREE(data); break; } diff --git a/cocos/2d/platform/linux/CCDevice.cpp b/cocos/2d/platform/linux/CCDevice.cpp index b062a658a7..a12d2ebc0c 100644 --- a/cocos/2d/platform/linux/CCDevice.cpp +++ b/cocos/2d/platform/linux/CCDevice.cpp @@ -411,7 +411,7 @@ public: int txtHeight = iMaxLineHeight; iMaxLineHeight = MAX(iMaxLineHeight, nHeight); - _data = new unsigned char[iMaxLineWidth * iMaxLineHeight * 4]; + _data = (unsigned char*)malloc(sizeof(unsigned char) * (iMaxLineWidth * iMaxLineHeight * 4)); memset(_data,0, iMaxLineWidth * iMaxLineHeight*4); int iCurYCursor = computeLineStartY(face, eAlignMask, txtHeight, iMaxLineHeight); diff --git a/cocos/2d/platform/mac/CCDevice.mm b/cocos/2d/platform/mac/CCDevice.mm index bf7b9099fc..5b83ee2e38 100644 --- a/cocos/2d/platform/mac/CCDevice.mm +++ b/cocos/2d/platform/mac/CCDevice.mm @@ -207,7 +207,7 @@ static bool _initWithString(const char * text, Device::TextAlign align, const ch NSUInteger textureSize = POTWide*POTHigh*4; - unsigned char* dataNew = new unsigned char[textureSize]; + unsigned char* dataNew = (unsigned char*)malloc(sizeof(unsigned char) * textureSize); if (dataNew) { memcpy(dataNew, data, textureSize); // output params diff --git a/cocos/2d/platform/win32/CCDevice.cpp b/cocos/2d/platform/win32/CCDevice.cpp index 1bf50cddef..9d3bf94ce2 100644 --- a/cocos/2d/platform/win32/CCDevice.cpp +++ b/cocos/2d/platform/win32/CCDevice.cpp @@ -412,7 +412,7 @@ Data Device::getTextureDataForText(const char * text,const FontDefinition& textD CC_BREAK_IF(! dc.drawText(text, size, align)); int dataLen = size.cx * size.cy * 4; - unsigned char* dataBuf = new unsigned char[dataLen]; + unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen); CC_BREAK_IF(! dataBuf); struct From b4121f47fcef6926c53d90b39594a72a0226ccf9 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 18 Feb 2014 11:07:58 +0800 Subject: [PATCH 2/4] [Android] Don't free BitmapDC::_data in BitmapDC's destructor. Remove unused BitmapDC::env. --- cocos/2d/platform/android/CCDevice.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/cocos/2d/platform/android/CCDevice.cpp b/cocos/2d/platform/android/CCDevice.cpp index d3136a41a3..b311c8cc8e 100644 --- a/cocos/2d/platform/android/CCDevice.cpp +++ b/cocos/2d/platform/android/CCDevice.cpp @@ -78,7 +78,6 @@ public: ~BitmapDC(void) { - CC_SAFE_FREE(_data); } bool getBitmapFromJavaShadowStroke( const char *text, @@ -112,11 +111,11 @@ public: // Do a full lookup for the font path using FileUtils in case the given font name is a relative path to a font file asset, // or the path has been mapped to a different location in the app package: std::string fullPathOrFontName = FileUtils::getInstance()->fullPathForFilename(pFontName); - - // If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context - // requires this portion of the path to be omitted for assets inside the app package. - if (fullPathOrFontName.find("assets/") == 0) - { + + // If the path name returned includes the 'assets' dir then that needs to be removed, because the android.content.Context + // requires this portion of the path to be omitted for assets inside the app package. + if (fullPathOrFontName.find("assets/") == 0) + { fullPathOrFontName = fullPathOrFontName.substr(strlen("assets/")); // Chop out the 'assets/' portion of the path. } @@ -160,7 +159,6 @@ public: int _width; int _height; unsigned char *_data; - JNIEnv *env; }; static BitmapDC& sharedBitmapDC() From 297fe597e1322448f1fdbbd1e4db04c7b748a5cd Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 18 Feb 2014 11:08:27 +0800 Subject: [PATCH 3/4] Updates mac / iOS project. --- build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index 283d017ac8..ab3eb20e53 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -5bde2def1fece71a2464526c90939b9999ed05f3 \ No newline at end of file +a7af0ccd05e86210026f7c72d6f5a7be8e17fe3d \ No newline at end of file From 34f8f0a3081ef844eda03f01878ae57d187e4797 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 18 Feb 2014 11:14:02 +0800 Subject: [PATCH 4/4] Uses `malloc` instead of `new[]`, keep malloc/free matching. --- cocos/2d/CCTexture2D.cpp | 56 ++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/cocos/2d/CCTexture2D.cpp b/cocos/2d/CCTexture2D.cpp index 3c69c7f614..29f672f94a 100644 --- a/cocos/2d/CCTexture2D.cpp +++ b/cocos/2d/CCTexture2D.cpp @@ -753,7 +753,7 @@ bool Texture2D::initWithImage(Image *image, PixelFormat format) if (outTempData != nullptr && outTempData != tempData) { - delete [] outTempData; + free(outTempData); } // set the premultiplied tag @@ -781,32 +781,32 @@ Texture2D::PixelFormat Texture2D::convertI8ToFormat(const unsigned char* data, s { case PixelFormat::RGBA8888: *outDataLen = dataLen*4; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertI8ToRGBA8888(data, dataLen, *outData); break; case PixelFormat::RGB888: *outDataLen = dataLen*3; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertI8ToRGB888(data, dataLen, *outData); break; case PixelFormat::RGB565: *outDataLen = dataLen*2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertI8ToRGB565(data, dataLen, *outData); break; case PixelFormat::AI88: *outDataLen = dataLen*2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertI8ToAI88(data, dataLen, *outData); break; case PixelFormat::RGBA4444: *outDataLen = dataLen*2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertI8ToRGBA4444(data, dataLen, *outData); break; case PixelFormat::RGB5A1: *outDataLen = dataLen*2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertI8ToRGB5A1(data, dataLen, *outData); break; default: @@ -830,37 +830,37 @@ Texture2D::PixelFormat Texture2D::convertAI88ToFormat(const unsigned char* data, { case PixelFormat::RGBA8888: *outDataLen = dataLen*2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertAI88ToRGBA8888(data, dataLen, *outData); break; case PixelFormat::RGB888: *outDataLen = dataLen/2*3; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertAI88ToRGB888(data, dataLen, *outData); break; case PixelFormat::RGB565: *outDataLen = dataLen; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertAI88ToRGB565(data, dataLen, *outData); break; case PixelFormat::A8: *outDataLen = dataLen/2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertAI88ToA8(data, dataLen, *outData); break; case PixelFormat::I8: *outDataLen = dataLen/2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertAI88ToI8(data, dataLen, *outData); break; case PixelFormat::RGBA4444: *outDataLen = dataLen; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertAI88ToRGBA4444(data, dataLen, *outData); break; case PixelFormat::RGB5A1: *outDataLen = dataLen; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertAI88ToRGB5A1(data, dataLen, *outData); break; default: @@ -885,32 +885,32 @@ Texture2D::PixelFormat Texture2D::convertRGB888ToFormat(const unsigned char* dat { case PixelFormat::RGBA8888: *outDataLen = dataLen/3*4; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGB888ToRGBA8888(data, dataLen, *outData); break; case PixelFormat::RGB565: *outDataLen = dataLen/3*2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGB888ToRGB565(data, dataLen, *outData); break; case PixelFormat::I8: *outDataLen = dataLen/3; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGB888ToI8(data, dataLen, *outData); break; case PixelFormat::AI88: *outDataLen = dataLen/3*2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGB888ToAI88(data, dataLen, *outData); break; case PixelFormat::RGBA4444: *outDataLen = dataLen/3*2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGB888ToRGBA4444(data, dataLen, *outData); break; case PixelFormat::RGB5A1: *outDataLen = dataLen; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGB888ToRGB5A1(data, dataLen, *outData); break; default: @@ -934,37 +934,37 @@ Texture2D::PixelFormat Texture2D::convertRGBA8888ToFormat(const unsigned char* d { case PixelFormat::RGB888: *outDataLen = dataLen/4*3; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGBA8888ToRGB888(data, dataLen, *outData); break; case PixelFormat::RGB565: *outDataLen = dataLen/2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGBA8888ToRGB565(data, dataLen, *outData); break; case PixelFormat::A8: *outDataLen = dataLen/4; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGBA8888ToA8(data, dataLen, *outData); break; case PixelFormat::I8: *outDataLen = dataLen/4; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGBA8888ToI8(data, dataLen, *outData); break; case PixelFormat::AI88: *outDataLen = dataLen/2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGBA8888ToAI88(data, dataLen, *outData); break; case PixelFormat::RGBA4444: *outDataLen = dataLen/2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGBA8888ToRGBA4444(data, dataLen, *outData); break; case PixelFormat::RGB5A1: *outDataLen = dataLen/2; - *outData = new unsigned char[*outDataLen]; + *outData = (unsigned char*)malloc(sizeof(unsigned char) * (*outDataLen)); convertRGBA8888ToRGB5A1(data, dataLen, *outData); break; default: @@ -1094,7 +1094,7 @@ bool Texture2D::initWithString(const char *text, const FontDefinition& textDefin if (outTempData != nullptr && outTempData != outData.getBytes()) { - delete [] outTempData; + free(outTempData); } #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX) _hasPremultipliedAlpha = true;