问题
Alright I am having a world of difficulty tracking down this memory leak. When running this script I do not see any memory leaking, but my objectalloc is climbing. Instruments points to CGBitmapContextCreateImage > create_bitmap_data_provider > malloc, this takes up 60% of my objectalloc.
This code is called several times with a NSTimer.
How do I clear that reUIImage after I return it?
...or How can I make it so that UIImage imageWithCGImage does not build my ObjectAlloc?
//I shorten the code because no one responded to another post
//Think my ObjectAlloc is building up on that retUIImage that I am returning
//**How do I clear that reUIImage after the return?**
-(UIImage) functionname {
//blah blah blah code
//blah blah more code
UIImage *retUIImage = [UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return retUIImage;
}
回答1:
this method you use instantiates a UIImage and sets it as autorelease. If you want to cleanup these, you will need to empty the pool periodically
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
..
..
..
[pool release];
Note that these can be nested:
NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];
..
..
..
[pool2 release];
[pool1 release];
Common practice is to place these around for loops and other methods that make many autoreleased objects.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (Thing *t in things) {
[thing doAMethodThatAutoreleasesABunchOfStuff];
}
[pool release]
来源:https://stackoverflow.com/questions/1430201/iphone-uiimage-leak-objectalloc-building