Release isn't working! The end of the world is here!
Repent, heretic! ^_^
Release is not doing what it's supposed to! I was happily working on my game when suddenly I've decided to watch how allocations are going on the device after making some changes at the images (avoiding .png image compression) and then, bang! The whole world crashed when I saw live memory is increasing as ever done. After seeking some explanation to the problem I've taken a memory printing function to see where exactly the problem is, and what a surprise when I saw that releasing functions are not freeing any space!
There are lot of images in the game so I've decided to load just the images that are going to be used and free useless images, then loading again when needed, etc.
I'm going to show you how all this works:
************************************************** ********
// First of all, the memory printing function:
- (void) print_free_memory {
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
[self print_free_memory];
}
---------------------------------------------------------------------------
// Once the game is finished, a function for releasing loaded textures is called:
- (void) netejaMosques
{
[self print_free_memory];
I've supposed that all the memory allocated in the first function is going to be released in the second function but, voilà! Memory increases when loading textures, but don't decreases when releasing textures! So, every time a game starts, same textures are loaded and it's memory space is NEVER freed (of course, there are much many functions that loads much many textures, but all of them are like this one, so it's the same case...).
So, as you can see, used memory increases when textures are loaded (of course), but used memory don't decreases when textures are released (eing?)!
I've also traced the textures retainCount and, as I've expected, retainCount just after calling release function is 1, so everything's OK...
What can be happening here? It have no sense! Is the end of the world here?
Any help you be madly appreciated! Thanks in advance!
Last edited by Johanovski; 12-23-2009 at 12:19 PM.
It's possible the Image class is doing some caching. I had a problem with images never being released in earlier versions of the OS, but that was with the ImageNamed method.
Try tracing the retaincount of an object you are trying to release; its possible you are somehow retaining it one more time than you think you are and thus the release call won't actually free the object in memory.
It's possible the Image class is doing some caching. I had a problem with images never being released in earlier versions of the OS, but that was with the ImageNamed method.
output the retain count of the Image instances you are releasing. I bet that it's not 1 and thus the release does not put it to zero.
__________________
regards
Oliver Drobnik Cocoanetics - Our DNA is programmed in Objective-C.
Cocoanetics Parts Store – easy to use yet professionally looking components that you can use to spruce up your own apps. Augmented Reality, Calendar Control, Pin Lock or Purchase Button are only some examples. You get full source code, no static library crap, and lifetime support. Check it out today!
Yeah; this is a common thing that is forgotten when you add an object to an array, for instance; the object is going to get retained again (above and beyond its original allocation).
I've checked array's (0,0) retainCount (which is an image) and it's 1 just before calling release. When trying to check after release an Exc bad acces is thrown (message sent to deallocated instance), so I've supposed release is correctly releasing the object (or maybe it just releases the array position? No idea of what is really doing, so it's not what I've thought since the beginning of the world...).
Something related to the array, you've said? Don't know why and what could be doing (or not doing)...
Any idea about this? I'm really at the edge of madness...
I'm trying to do what you've said (or what I think you've said) but I'm not able to... If I'm not wrong (and I think I'm ^_^) what I have to do is to allocate each UIImage before initializing the Image class and then releasing the UIImage, right? So, it should be something like this:
But it's not possible to allocate a UIImage (don't know why, but it's not possible), so I have no idea of what you're meaning... Sorry, I'm quite new to iPhone development and OpenGL...
I thought method "imageWithContentsOfFile" didn't cache the image, so there's no need to release it... The returned image is needed to be allocated because it's being used every game's frame, so I can't release the Image class (the one is stored in the array) but, as I've explained, I thought the UIImage is never allocated, just created, returned, stored in the Image class as I've expected, and then destroyed (so a UIImage object is created, a Image object is then allocated and created from the UIImage returned object, and then no UIImage is stored)...
Does it works as I expect? If not (what I suppose), what I have to do to release the UIImage, if I have no object to call release (and no idea of how to autorelease an object, and even if it's possible)?
I thought method "imageWithContentsOfFile" didn't cache the image, so there's no need to release it...
Caching and needing to release are not the same thing.
[UIImage imageWithContentsOfFile] returns a UIImage*, this points to a UIImage object containing the image you requested. You need to release this yourself, to do this you just need to store the returned UIImage* and then call release on it when you are done with it. You do not need to call alloc because initWithContentsOfFile has already allocated the object.
Now, had you used imageNamed instead it would return a pointer to a UIImage object you would need to release and it would internally cache a copy of the image so that the next time you call ImageNamed it will load near instantly. That's what caching is.
Quote:
...I thought the UIImage is never allocated, just created, returned, stored...
How did you expect it to be created without being allocated?
__________________
Visit Mr Jack Games for my blog and more about my games
So a UIImage* is created, the UIImage is allocated inside it's own method (so no additional alloc is called), then the Image class is initialized with a UIImage, and after this the UIImage* is released... I think this is the theory, more or less, but when I try to run the app...
----------------------------------------------------------------------
[1541:207] *** -[UIImage release]: message sent to deallocated instance 0x3b37a20
----------------------------------------------------------------------
Seems that log says UIImage is never allocated, so releasing is not possible... it's losing sense as time passes!
Back from Christmas, and having to face the same (still unsolved) problem again... As it seems it have no sense that releasing images don't free memory at all, I've tried doing a test function to ensure that allocating and releasing variables is really affecting memory as it should do, so I've done the following:
What the #%&"?? Seems that used memory don't decrease even when I release 400 allocated NSStrings? Does it really have sense...?
Thanks, any help will be really appreciated!
Change that to mutableStrings and you'll probably get the results you're expecting. [[NSString alloc] initWithString:@"Yabadabadoo!"] will have the same result as just writing @"Yabadabadoo!" , which is a static string that doesn't get deallocated at all during the life of your program.
RE: your images releasing, I think someone pointed out in another thread that your Texture2D dealloc call was not deleting the openGL texture.