0

好吧,我很难追踪这个内存泄漏。运行此脚本时,我没有看到任何内存泄漏,但我的 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;
    }
4

1 回答 1

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]
于 2009-09-16T01:00:59.180 回答