I have an class method which generates a UIImage, like this:
+ (UIImage*)imageWithFileName:(NSString*)imgFile {
UIImage *img = nil;
NSBundle *appBundle = [NSBundle mainBundle];
NSString *resourcePath = [appBundle pathForResource:imgFile ofType:nil];
if (resourcePath != nil) {
NSURL *imageURL = [NSURL fileURLWithPath:resourcePath];
NSData *data = [[NSData alloc] initWithContentsOfURL:imageURL];
img = [UIImage imageWithData:data]; // should be autoreleased!!
[data release];
}
return img;
}
However, when I use this, the image data is NEVER freed. There is definitely a memory bug with this, although I didn’t break any memory management rule I am aware of. My guess is that because this is a class method which gets called from instance methods, There is no active autorelease pool in place or it’s one that only gets drained when I quit the app. Could that be right?
The real question I think is, how are you measuring that the memory is not released.
Autorelease pools are all related to the thread and runloop you are in – because they free memory when a call returns all the way to the main runloop. It doesn’t matter if you are calling a class method or an instance method or even a C function, autorelease will work the same in all cases.
I know in your testing you found there are differences, but simply put if you see a difference it is for some other reason – because autorelease always works the same across the system as far as freeing memory if you are in the same runloop.