From 294341d0abaf8ea4c5069115e3a1a33bbd63d2f0 Mon Sep 17 00:00:00 2001 From: Dale Stammen Date: Sat, 22 Mar 2014 05:56:08 -0700 Subject: [PATCH] fixed texture memory leak and added comments to indicate problem --- cocos/2d/CCFontAtlas.cpp | 12 +++++++++--- cocos/2d/CCTexture2D.cpp | 11 +++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cocos/2d/CCFontAtlas.cpp b/cocos/2d/CCFontAtlas.cpp index 3e40e41419..0710d13566 100644 --- a/cocos/2d/CCFontAtlas.cpp +++ b/cocos/2d/CCFontAtlas.cpp @@ -185,7 +185,9 @@ void FontAtlas::listenToForeground(EventCustom *event) auto contentSize = Size(CacheTextureWidth,CacheTextureHeight); auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; - _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize ); + // this is a memory leak as the texture previously in _atlasTextures[_currentPage] is not deleted from OpenGL + // see CCTexture2D::initWithData for the temporary fix + _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize ); } } #endif @@ -254,8 +256,10 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) _currentPageOrigY += _commonLineHeight; _currentPageOrigX = 0; if(_currentPageOrigY + _commonLineHeight >= CacheTextureHeight) - { - _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize ); + { + // this is a memory leak as the texture previously in _atlasTextures[_currentPage] is not deleted from OpenGL + // see CCTexture2D::initWithData for the temporary fix + _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize ); _currentPageOrigY = 0; memset(_currentPageData, 0, _currentPageDataSize); _currentPage++; @@ -299,6 +303,8 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String) if(existNewLetter) { + // this is a memory leak as the texture previously in _atlasTextures[_currentPage] is not deleted from OpenGL + // see CCTexture2D::initWithData for the temporary fix _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize ); } return true; diff --git a/cocos/2d/CCTexture2D.cpp b/cocos/2d/CCTexture2D.cpp index a43563d862..54c59ec088 100644 --- a/cocos/2d/CCTexture2D.cpp +++ b/cocos/2d/CCTexture2D.cpp @@ -51,6 +51,8 @@ THE SOFTWARE. NS_CC_BEGIN + + namespace { typedef Texture2D::PixelFormatInfoMap::value_type PixelFormatInfoMapValue; static const PixelFormatInfoMapValue TexturePixelFormatInfoTablesValue[] = @@ -533,6 +535,15 @@ bool Texture2D::hasPremultipliedAlpha() const bool Texture2D::initWithData(const void *data, ssize_t dataLen, Texture2D::PixelFormat pixelFormat, int pixelsWide, int pixelsHigh, const Size& contentSize) { + // cocos2d-x is currently calling this multiple times on the same Texture2D + // if the GL texture has already been created,it will be leaked in OpenGL + // For now, call deleteTexture if the texture already exists + if(_name) + { + GL::deleteTexture(_name); + _name = 0; + } + CCASSERT(dataLen>0 && pixelsWide>0 && pixelsHigh>0, "Invalid size"); //if data has no mipmaps, we will consider it has only one mipmap