Game Development Community

Memory leak in t2dTileLayer objects (iTGB 1.4) [SOLVED]

by Richard Skala · in iTorque 2D · 12/11/2010 (11:21 pm) · 6 replies

I've been running the "Leaks" Instrument to detect any memory leaks running the game, and it detected a leak in the following function:

t2dTileLayer::createLayer(unsigned int, unsigned int, float, float)

which was called from:

t2dTileLayer::setLayerFile(void*, char const*)

Does anyone know anything about this? I haven't found any related threads regarding it. Any help is greatly appreciated.

#1
12/11/2010 (11:54 pm)
So, I figured out how to fix the memory leak:

=====================================
SOLUTION
=====================================
In the function:

bool t2dTileLayer::setLayerFile(void* obj, const char* data)

Comment out these two lines in both of the "else-if" blocks:

layer->deleteLayer();
layer->createLayer(1, 1, 1, 1);

=====================================
CAUSE
=====================================

It seems those two lines are unnecessary (for my purposes at least), as this call in that same block:
layer->loadTileLayer(data);

Will end up calling:
IMPLEMENT_T2D_LOAD_METHOD( t2dTileLayer, 3 )

Which ends up calling:
object->createLayer( tileCountX, tileCountY, tileSizeX, tileSizeY );

This means the second call to createLayer() is allocating mppTileObjectArray without freeing it first. So, I suppose another solution would be to call deleteLayer() before the createLayer() in that IMPLEMENT_T2D_LOAD_METHOD(), although the first seems pointless.
#2
12/12/2010 (1:38 am)
I see that createlayer does not make a sense, but why would you not call delete unless you want a memory leak cause the old one is not gone?
#3
12/12/2010 (2:13 am)
True. It is probably a good idea to put object->deleteLayer() before the call to object->createLayer(...) in that load method.
#4
04/28/2011 (3:43 am)
@Richard,@Marc

Sorry for bumping this old thread,but I have a question.

In regards to the "Leaks" Instrument -- is this something within ITGB 1.41?
or is this something in Xcode?

Any Links or info about this would be appreciated.
#5
04/28/2011 (4:19 am)
This is an Xcode tool.

http://developer.apple.com/library/mac/#documentation/Performance/Conceptual/ManagingMemory/Articles/FindingLeaks.html
#6
04/28/2011 (6:45 pm)
@Alistair

Thanks You. :D