【问题标题】:iPhone - UIImage Leak, ObjectAlloc BuildingiPhone - UIImage 泄漏,ObjectAlloc 构建
【发布时间】:2009-09-15 23:45:11
【问题描述】:

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

【问题讨论】:

    标签: iphone memory-leaks uiimage cgbitmapcontextcreate


    【解决方案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]
    

    【讨论】:

    • 我见过NSAutoreleasePool 是这样发布的...[pool drain] 哪种方式是正确的? [水池排水] 还是 [水池释放]?
    • 来自文档:在引用计数环境中,此方法的行为与发布相同。由于无法保留自动释放池(请参阅保留),因此这会导致接收器被释放。当自动释放池被释放时,它会向其所有自动释放的对象发送释放消息。如果一个对象被多次添加到同一个池中,当池被释放时,每次添加时都会收到一条释放消息。手机没有垃圾回收
    • 评论不清楚,但该描述来自排水法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-26
    • 1970-01-01
    相关资源
    最近更新 更多