diff --git a/native/cocos/2d/CCAutoPolygon.cpp b/native/cocos/2d/CCAutoPolygon.cpp index 910b922481..0b5c1e8123 100644 --- a/native/cocos/2d/CCAutoPolygon.cpp +++ b/native/cocos/2d/CCAutoPolygon.cpp @@ -169,7 +169,7 @@ AutoPolygon::AutoPolygon(const std::string &filename) AutoPolygon::~AutoPolygon() { - CC_SAFE_DELETE(_image); + CC_SAFE_RELEASE(_image); } std::vector AutoPolygon::trace(const Rect& rect, const float& threshold) diff --git a/native/cocos/2d/CCRenderTexture.cpp b/native/cocos/2d/CCRenderTexture.cpp index df3b5c072c..212da76fd5 100644 --- a/native/cocos/2d/CCRenderTexture.cpp +++ b/native/cocos/2d/CCRenderTexture.cpp @@ -85,7 +85,7 @@ RenderTexture::~RenderTexture() { glDeleteRenderbuffers(1, &_stencilRenderBufffer); } - CC_SAFE_DELETE(_UITextureImage); + CC_SAFE_RELEASE_NULL(_UITextureImage); } void RenderTexture::listenToBackground(EventCustom *event) @@ -93,7 +93,7 @@ void RenderTexture::listenToBackground(EventCustom *event) // We have not found a way to dispatch the enter background message before the texture data are destroyed. // So we disable this pair of message handler at present. #if CC_ENABLE_CACHE_TEXTURE_DATA - CC_SAFE_DELETE(_UITextureImage); + CC_SAFE_RELEASE(_UITextureImage); // to get the rendered texture data _UITextureImage = newImage(false); @@ -510,7 +510,7 @@ void RenderTexture::onSaveToFile(const std::string& filename, bool isRGBA) { _saveFileCallback(this, filename); } - CC_SAFE_DELETE(image); + CC_SAFE_RELEASE_NULL(image); } /* get buffer as Image */ diff --git a/native/cocos/2d/CCSpriteFrameCache.cpp b/native/cocos/2d/CCSpriteFrameCache.cpp index 6e58ca79ac..6b20790ba8 100644 --- a/native/cocos/2d/CCSpriteFrameCache.cpp +++ b/native/cocos/2d/CCSpriteFrameCache.cpp @@ -293,7 +293,7 @@ void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dictionary, Textu // add sprite frame _spriteFrames.insert(spriteFrameName, spriteFrame); } - CC_SAFE_DELETE(image); + CC_SAFE_RELEASE(image); } void SpriteFrameCache::addSpriteFramesWithDictionary(ValueMap& dict, const std::string &texturePath) diff --git a/native/cocos/base/ccUtils.cpp b/native/cocos/base/ccUtils.cpp index b7740d3dba..18d0ef33d4 100644 --- a/native/cocos/base/ccUtils.cpp +++ b/native/cocos/base/ccUtils.cpp @@ -140,7 +140,7 @@ void onCaptureScreen(const std::function& afterC AsyncTaskPool::getInstance()->enqueue(AsyncTaskPool::TaskType::TASK_IO, mainThread, nullptr, [image, outputFile]() { succeedSaveToFile = image->saveToFile(outputFile); - delete image; + image->release(); }); } else diff --git a/native/cocos/platform/CCImage.cpp b/native/cocos/platform/CCImage.cpp index 2330c549d4..cc06a7b8c0 100644 --- a/native/cocos/platform/CCImage.cpp +++ b/native/cocos/platform/CCImage.cpp @@ -374,21 +374,6 @@ bool Image::initWithImageFile(const std::string& path) return ret; } -bool Image::initWithImageFileThreadSafe(const std::string& fullpath) -{ - bool ret = false; - _filePath = fullpath; - - Data data = FileUtils::getInstance()->getDataFromFile(fullpath); - - if (!data.isNull()) - { - ret = initWithImageData(data.getBytes(), data.getSize()); - } - - return ret; -} - bool Image::initWithImageData(const unsigned char * data, ssize_t dataLen) { bool ret = false; diff --git a/native/cocos/platform/CCImage.h b/native/cocos/platform/CCImage.h index 3917c0497b..6449303bf5 100644 --- a/native/cocos/platform/CCImage.h +++ b/native/cocos/platform/CCImage.h @@ -58,16 +58,10 @@ typedef struct _MipmapInfo class CC_DLL Image : public Ref { public: - friend class TextureCache; /** * @js ctor */ Image(); - /** - * @js NA - * @lua NA - */ - virtual ~Image(); /** Supported formats for Image */ enum class Format @@ -197,17 +191,18 @@ protected: protected: // noncopyable - Image(const Image& rImg); - Image& operator=(const Image&); + Image(const Image&) = delete; + Image& operator=(const Image&) = delete; - /* - @brief The same result as with initWithImageFile, but thread safe. It is caused by - loadImage() in TextureCache.cpp. - @param fullpath full path of the file. - @param imageType the type of image, currently only supporting two types. - @return true if loaded correctly. + // nonmoveable + Image(Image&&) = delete; + Image& operator=(Image&&) = delete; + + /** + * @js NA + * @lua NA */ - bool initWithImageFileThreadSafe(const std::string& fullpath); + virtual ~Image(); Format detectFormat(const unsigned char * data, ssize_t dataLen); bool isPng(const unsigned char * data, ssize_t dataLen); diff --git a/native/cocos/renderer/CCTexture2D.h b/native/cocos/renderer/CCTexture2D.h index da445f1a47..90061efaad 100644 --- a/native/cocos/renderer/CCTexture2D.h +++ b/native/cocos/renderer/CCTexture2D.h @@ -186,11 +186,7 @@ public: * @js ctor */ Texture2D(); - /** - * @js NA - * @lua NA - */ - virtual ~Texture2D(); + /** Get texture name, dimensions and coordinates message by a string. * @js NA @@ -394,6 +390,20 @@ public: static const PixelFormatInfoMap& getPixelFormatInfoMap(); private: + // noncopyable + Texture2D(const Texture2D&) = delete; + Texture2D& operator=(const Texture2D&) = delete; + + // nonmoveable + Texture2D(Texture2D&&) = delete; + Texture2D& operator=(Texture2D&&) = delete; + + /** + * @js NA + * @lua NA + */ + virtual ~Texture2D(); + /** * A struct for storing 9-patch image capInsets. */ diff --git a/native/cocos/renderer/CCTextureCache.cpp b/native/cocos/renderer/CCTextureCache.cpp index 30ef33b4fe..0934551641 100644 --- a/native/cocos/renderer/CCTextureCache.cpp +++ b/native/cocos/renderer/CCTextureCache.cpp @@ -83,12 +83,25 @@ std::string TextureCache::getDescription() const struct TextureCache::AsyncStruct { public: - AsyncStruct(const std::string& fn, std::function f) : filename(fn), callback(f), pixelFormat(Texture2D::getDefaultAlphaPixelFormat()), loadSuccess(false) {} + AsyncStruct(const std::string& fn, std::function f) + : filename(fn) + , callback(f) + , image(new (std::nothrow) Image()) + , imageAlpha(new (std::nothrow) Image()) + , pixelFormat(Texture2D::getDefaultAlphaPixelFormat()) + , loadSuccess(false) + {} + + ~AsyncStruct() + { + CC_SAFE_RELEASE(image); + CC_SAFE_RELEASE(imageAlpha); + } std::string filename; std::function callback; - Image image; - Image imageAlpha; + Image* image; + Image* imageAlpha; Texture2D::PixelFormat pixelFormat; bool loadSuccess; }; @@ -223,7 +236,7 @@ void TextureCache::loadImage() } // load image - asyncStruct->loadSuccess = asyncStruct->image.initWithImageFileThreadSafe(asyncStruct->filename); + asyncStruct->loadSuccess = asyncStruct->image->initWithImageFile(asyncStruct->filename); // push the asyncStruct to response queue _responseMutex.lock(); @@ -270,7 +283,7 @@ void TextureCache::addImageAsyncCallBack(float dt) // convert image to texture if (asyncStruct->loadSuccess) { - Image* image = &(asyncStruct->image); + Image* image = asyncStruct->image; // generate texture in render thread texture = new (std::nothrow) Texture2D(); @@ -638,7 +651,7 @@ void TextureCache::renameTextureWithKey(const std::string& srcName, const std::s _textures.insert(std::make_pair(fullpath, tex)); _textures.erase(it); } - CC_SAFE_DELETE(image); + CC_SAFE_RELEASE(image); } } }