Hi there!
I've just done a mega-memory trace of my app and I've found that every time game's images are loaded (each time you start the game from the main menu) living memory increases like 1 or 1.5 MB, which is quite critical! I've traced where these allocations came from and I've found that come from OpenGL's function glTexImage2D. Each call on this function allocates a 1MB Malloc which seems not to be released after that, so it keeps alive...
I've searched the whole code and I've found that this method is only used in a function inside the Texture2D.m class. Here is the function and the dealloc method (I think last one is correct, but I'm not sure so I copy it too):
Code:
// Texture2D.m
- (id) initWithData:(const void*)data pixelFormat:(Texture2DPixelFormat)pixelFormat pixelsWide:(NSUInteger)width pixelsHigh:(NSUInteger)height contentSize:(CGSize)size
{
GLint saveName;
if((self = [super init])) {
glGenTextures(1, &_name);
glGetIntegerv(GL_TEXTURE_BINDING_2D, &saveName);
glBindTexture(GL_TEXTURE_2D, _name);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
switch(pixelFormat) {
case kTexture2DPixelFormat_RGBA8888:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
break;
case kTexture2DPixelFormat_RGB565:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, data);
break;
case kTexture2DPixelFormat_A8:
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, data);
break;
default:
[NSException raise:NSInternalInconsistencyException format:@""];
}
glBindTexture(GL_TEXTURE_2D, saveName);
_size = size;
_width = width;
_height = height;
_format = pixelFormat;
_maxS = size.width / (float)width;
_maxT = size.height / (float)height;
}
return self;
}
And the dealloc method:
Code:
- (void) dealloc
{
if(_name)
glDeleteTextures(1, &_name);
[super dealloc];
}
I've also found a strange thing that I don't know how it works and why it's like is...
The Texture2D.m class (which I've taken from a tutorial) seems to be split in some parts divided by the "@implementation" and "@end" words, so there are one for "Texture2D", one for "Texture2D (Image)", one for "Texture2D (Text)", and one for "Texture2D (Drawing)", and I have to say that only the "Texture2D" and the "Texture2D (Drawing)" parts have a dealloc method before the "@end" line, I don't know it's what can be causing the leaks (sorry, maybe it's a quite newbie question, but I don't know exactly how this works and I'm get easily lost with this)...
Thanks in advance for your help!