iPhone - UIImage Leak,ObjectAlloc Building

时间:2009-09-15 23:45:12

标签: iphone memory-leaks uiimage cgbitmapcontextcreate

好吧,我有一个难以追踪内存泄漏的世界。运行此脚本时,我没有看到任何内存泄漏,但我的objectalloc正在攀爬。 Instruments指向CGBitmapContextCreateImage> create_bitmap_data_provider> malloc,这占我的objectalloc的60%。

使用NSTimer多次调用此代码。

退回后如何清除reUIImage?

...或者我怎样才能使UIImage imageWithCGImage不构建我的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 个答案:

答案 0 :(得分:1)

您使用此方法实例化UIImage并将其设置为自动释放。如果要清理这些,则需要定期清空池

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
..
..
..
[pool release];

请注意,这些可以嵌套:

NSAutoreleasePool *pool1 = [[NSAutoreleasePool alloc] init];
NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init];
..
..
..
[pool2 release];
[pool1 release];

通常的做法是将这些放在for循环和其他生成许多自动释放对象的方法中。

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (Thing *t in things) {
  [thing doAMethodThatAutoreleasesABunchOfStuff];
}
[pool release]