iOS应用程序崩溃时UIImage加载(虚拟内存不清理) [英] iOS Application Crash while UIImage loading (virtual memory not cleaning up)

查看:268
本文介绍了iOS应用程序崩溃时UIImage加载(虚拟内存不清理)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的崩溃在我的应用程序没有任何痕迹。这可能是一个记忆相关的问题,但信息非常少。我不知道如何继续或解决它。如果它不是仪器将没有任何线索什么这么久。

I have a weird crash in my application without any trace. this is probably a memory related problem but with very little information & I'm not sure how to proceed or fix it. If it wasnt for instruments would have been left with no clue what so ever.

我有一个图像数组(在这个例子中是一个大小为2的数组),其中我加载一个图像,创建一个图像上下文&绘制并保存到数组中。每次该方法被称为图像数组对象被替换为新的内容。在仪器中,我看到一个非常巨大的虚拟内存使用在这个方法调用&显然在每个呼叫存储器未被清除&因此崩溃。该项目是ARC。我将在下面列出代码。这是我们需要重新创建这个问题。 (我使用的图像大小约7MB,所以它更容易重新创建崩溃)。我还在使用iPad2设备。

I have an image array (in this example an array of size 2) where I load an image, create an image context & draw and save it into the array. Everytime the method is called image array objects are replaced with the new content. In instruments I see a very huge Virtual Memory usage during this method call & apparently after each call memory is not cleared & hence crashes. The project is ARC. I'll list down code below. This is all we need to recreate this issue. (the image I'm using is little big in size about 7MB, so its easier to recreate crash). Also i'm using iPad2 device.

+ (UIImage *)imageCopy:(UIImage *)src
{
    UIGraphicsBeginImageContext(src.size);
    [src drawAtPoint:CGPointZero];
    UIImage *r = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return r;
} 

- (IBAction)buttonTouch:(id)sender
{
    for (int i=0; i<2; i++)
    {
        if (i==0)
        {
            self.mImage = [UIImage imageNamed:@"LARGE_elevation.jpg"];
        }
        else
        {
            self.mImage = [UIImage imageNamed:@"LARGE_elevation2.jpg"];
        }
        // imageArray is a NSMutableArray with capacity of 2
        [imageArray setObject:[ViewController imageCopy:self.mImage] atIndexedSubscript:i];
    }
    ((UIImageView *)[self.view viewWithTag:100]).image = self.mImage;
}

这里是一个屏幕从仪器,它在第二次崩溃后内存警告发行。

Here is a screen from instruments where it crash on 2nd time after memory warnings are issued.

我没有看到imageCopy方法的任何大问题我在这里使用。

I dont see any big issue with the "imageCopy" method I'm using here.

对此有任何帮助,非常感谢。
谢谢& Cheers,

Any help on this is really appreciated. Thanks & Cheers,

推荐答案

我发现这是一个循环引用 问题。因此,当新内容替换数组中的旧内容时,过去的对象仍然保留。这是一个非常有趣的发现,因为在内存泄漏分析器中,它显示了很少的KB数据泄漏,你不会怀疑,因为非释放的虚拟内存是几百兆字节(MB)。

I found out that it was a cyclic reference issue. So when new content replaces the old content in the array, the past objects were still remaining. It was quite an interesting finding because in the memory leaks analyser it showed up as few KB data leak which you wont suspect as the non freed virtual memory was few hundred Megabytes (MB).

作为一个抽象的例子。

As a very abstract example.

ClassA

@property (strong) ClassB *obj

----------

ClassB

@property (strong) ClassA *obj

- (id)initWithA:(ClassA *)objA;
----------

将被正确解除分配。在我的情况下,泄漏分析仪泄漏跟踪的几个KB的两个对象,即使CoreGraphics计算挂在约200MB的数据在虚拟内存。

So when you remove A neither object will be deallocated properly. In my case leak traced by the leak analyser was few KB for both of the objects even though the CoreGraphics calculations were hanging onto about 200MB data in virtual memory.

修复是将ClassB中的A引用标记为weak。

Fix was to mark the A reference in ClassB as weak.

ClassB

@property (weak) ClassA *obj

- (id)initWithA:(ClassA *)objA;

判决


  • 不要估计内存泄漏,无论大小, arc或mrc

这篇关于iOS应用程序崩溃时UIImage加载(虚拟内存不清理)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆