fixed texture memory leak and added comments to indicate problem

This commit is contained in:
Dale Stammen 2014-03-22 05:56:08 -07:00
parent 65400bf87b
commit 294341d0ab
2 changed files with 20 additions and 3 deletions

View File

@ -185,7 +185,9 @@ void FontAtlas::listenToForeground(EventCustom *event)
auto contentSize = Size(CacheTextureWidth,CacheTextureHeight); auto contentSize = Size(CacheTextureWidth,CacheTextureHeight);
auto pixelFormat = fontTTf->getOutlineSize() > 0 ? Texture2D::PixelFormat::AI88 : Texture2D::PixelFormat::A8; 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 #endif
@ -255,7 +257,9 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
_currentPageOrigX = 0; _currentPageOrigX = 0;
if(_currentPageOrigY + _commonLineHeight >= CacheTextureHeight) 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; _currentPageOrigY = 0;
memset(_currentPageData, 0, _currentPageDataSize); memset(_currentPageData, 0, _currentPageDataSize);
_currentPage++; _currentPage++;
@ -299,6 +303,8 @@ bool FontAtlas::prepareLetterDefinitions(unsigned short *utf16String)
if(existNewLetter) 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 ); _atlasTextures[_currentPage]->initWithData(_currentPageData, _currentPageDataSize, pixelFormat, CacheTextureWidth, CacheTextureHeight, contentSize );
} }
return true; return true;

View File

@ -51,6 +51,8 @@ THE SOFTWARE.
NS_CC_BEGIN NS_CC_BEGIN
namespace { namespace {
typedef Texture2D::PixelFormatInfoMap::value_type PixelFormatInfoMapValue; typedef Texture2D::PixelFormatInfoMap::value_type PixelFormatInfoMapValue;
static const PixelFormatInfoMapValue TexturePixelFormatInfoTablesValue[] = 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) 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"); CCASSERT(dataLen>0 && pixelsWide>0 && pixelsHigh>0, "Invalid size");
//if data has no mipmaps, we will consider it has only one mipmap //if data has no mipmaps, we will consider it has only one mipmap