Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-08-2010, 07:33 AM   #1 (permalink)
Divine avenger
 
Johanovski's Avatar
 
Join Date: Nov 2009
Location: Vic, Catalunya (Spain)
Posts: 320
Default glTexImage2D leaking like hell, any help?

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!
Johanovski is offline   Reply With Quote
Old 01-08-2010, 11:42 AM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Increasing memory by 1MB sounds about right for a 512x512 texture. The memory should be returned when you release the Texture2D and the dealloc is called but the system, though. The the glDeleteTextures line will delete the texture which should free the memory.

Regarding the Texture2D (Image) Texture2D (Text) Texture2D (Drawing), the author of the file used "categories" to divide up the implementation. It's OK that there's only one dealloc, because it's still just one class.

EDIT: I'd add an NSLog in the dealloc to find out if it's getting called. If dealloc is never getting called then the memory is never getting freed.
__________________

Free Games!

Last edited by smasher; 01-08-2010 at 12:15 PM.
smasher is offline   Reply With Quote
Old 01-08-2010, 12:02 PM   #3 (permalink)
Maker of Games
 
Mr Jack's Avatar
 
Join Date: Nov 2009
Location: Coventry, UK
Posts: 395
Default

Why are you re-loading the images each time? Why not load them once, during start up, and keep the memory resident throughout the time? From your other threads it seems you only have 20mb or so of images? This should be a small enough amount to load once.

As for why it's leaking, have you checked that you're calling dealloc on all the images you load?
__________________


Visit Mr Jack Games for my blog and more about my games
Mr Jack is offline   Reply With Quote
Reply

Bookmarks

Tags
glteximage2d, implementation, memory leaking, release, texture2d

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,765
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:34 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0