From 98146f438b8301bf83c53591cf9819dc4aa13332 Mon Sep 17 00:00:00 2001 From: RH Date: Fri, 28 Apr 2023 16:54:57 +1000 Subject: [PATCH] Fix RenderTexture memory leak and increased memory usage when initWithWidthAndHeight is called (#1191) --- core/2d/CCRenderTexture.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/core/2d/CCRenderTexture.cpp b/core/2d/CCRenderTexture.cpp index 0f0a957e8f..6b0302a5f5 100644 --- a/core/2d/CCRenderTexture.cpp +++ b/core/2d/CCRenderTexture.cpp @@ -198,6 +198,8 @@ bool RenderTexture::initWithWidthAndHeight(int w, _renderTargetFlags = RenderTargetFlag::ALL; descriptor.textureFormat = PixelFormat::D24S8; + AX_SAFE_RELEASE(_depthStencilTexture); + _depthStencilTexture = new Texture2D(); _depthStencilTexture->updateTextureDescriptor(descriptor); } @@ -271,8 +273,13 @@ void RenderTexture::setSprite(Sprite* sprite) sEngine->releaseScriptObject(this, _sprite); } #endif // AX_ENABLE_GC_FOR_NATIVE_OBJECTS + if (_sprite) + { + _sprite->removeFromParent(); + _sprite->release(); + } + AX_SAFE_RETAIN(sprite); - AX_SAFE_RELEASE(_sprite); _sprite = sprite; }