diff --git a/cocos/2d/platform/CCImage.cpp b/cocos/2d/platform/CCImage.cpp index b9ef38f2d6..0b6e21874e 100644 --- a/cocos/2d/platform/CCImage.cpp +++ b/cocos/2d/platform/CCImage.cpp @@ -804,14 +804,7 @@ bool Image::initWithJpgData(const unsigned char * data, ssize_t dataLen) _dataLen = cinfo.output_width*cinfo.output_height*cinfo.output_components; _data = static_cast(malloc(_dataLen * sizeof(unsigned char))); - if(!_data) - { - if (row_pointers != nullptr) - { - free(row_pointers); - } - break; - } + CC_BREAK_IF(! _data); /* now actually read the jpeg into the raw buffer */ /* read one scan line at a time */ @@ -949,7 +942,14 @@ bool Image::initWithPngData(const unsigned char * data, ssize_t dataLen) _dataLen = rowbytes * _height; _data = static_cast(malloc(_dataLen * sizeof(unsigned char))); - CC_BREAK_IF(!_data); + if(!_data) + { + if (row_pointers != nullptr) + { + free(row_pointers); + } + break; + } for (unsigned short i = 0; i < _height; ++i) { @@ -964,7 +964,7 @@ bool Image::initWithPngData(const unsigned char * data, ssize_t dataLen) if (row_pointers != nullptr) { free(row_pointers); - }; + } bRet = true; } while (0); @@ -2035,6 +2035,9 @@ bool Image::saveImageToPNG(const std::string& filePath, bool isToRGB) { fclose(fp); png_destroy_write_struct(&png_ptr, &info_ptr); + + free(row_pointers); + row_pointers = nullptr; break; }