仪器指向内存泄漏!

发布于 2024-08-04 10:46:47 字数 520 浏览 5 评论 0原文

嗨,我有这个代码用于初始化我的类。

- (id)initWithSize:(int)size {

    self = [super init];

    if( self != nil )
    {
        [self setMyVector:[[NSMutableArray alloc] initWithCapacity:size]];

        for ( int i = 0; i < size; i++ ) 
        {
            [myVector addObject: [[NSMutableArray alloc] initWithCapacity:size]];
        }
    }

    return self;
}

我在 Instruments 中遇到了这个泄漏!
类别:CFArray(存储双端队列)
事件类型:Malloc

有人知道我需要什么来解决它吗? 谢谢!

Hi, i'm have this code for initialize my class.

- (id)initWithSize:(int)size {

    self = [super init];

    if( self != nil )
    {
        [self setMyVector:[[NSMutableArray alloc] initWithCapacity:size]];

        for ( int i = 0; i < size; i++ ) 
        {
            [myVector addObject: [[NSMutableArray alloc] initWithCapacity:size]];
        }
    }

    return self;
}

I'm get this leak in Instruments !
Category: CFArray (store-deque)
Event Type: Malloc

Anyone know what i need to resolve it ?
Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞别 2024-08-11 10:46:47

泄漏发生在您的 for 循环中,也可能发生在之前。

首先,这一行:

[self setMyVector:[[NSMutableArray alloc] initWithCapacity:size]];

如果 setMyVector 保留传递的数组(根据约定,它可能应该),那么你就泄露了数组。首先分配并初始化数组。它的保留计数为+1。然后将其设置到 myVector 实例变量中,这意味着它的保留计数为 +2。当您将其他内容放入 myVector 中,或者在 dealloc 方法中释放 myVector 变量时,您将减少保留计数,这意味着它将有 +1 的保留计数。换句话说,它不会被释放,并且您已经泄漏了数组。

另一个明确的泄漏是在 for() 循环内,其中有:

[myVector addObject: [[NSMutableArray alloc] initWithCapacity:size]];

再次,您创建了一个保留计数为+1,然后将其添加到 myVector 数组中,该数组将再次保留它 (+2)。但是,您不再拥有指向该数组的指针,因此您已经泄漏了它。

这两个泄漏都可以通过使用 [NSMutableArray arrayWithCapacity:size] 来解决,而不是使用 alloc/init 方法。这将创建一个自动释放的数组,这将解决您的内存泄漏问题。

如果您不知道什么是 autoreleasing,那么您可能需要考虑创建数组并将其存储到局部变量中,将其添加到 myVector (或将其设置到 myVector 中),然后立即释放它。

The leak is in your for loop, and possibly also one before.

First, this line:

[self setMyVector:[[NSMutableArray alloc] initWithCapacity:size]];

If setMyVector retains the passed array (which, according to convention, it probably should), then you've leaked the array. First you alloc and init the array. It has a retain count of +1. Then you set it into the myVector instance variable, which means it has a retain count of +2. When you put something else into myVector, or you release the myVector variable in your dealloc method, you're going to decrement the retain count, which means it'll have a retain count of +1. In other words, it won't be deallocated, and you've leaked the array.

The other, definite leak is inside your for() loop, where you have:

[myVector addObject: [[NSMutableArray alloc] initWithCapacity:size]];

Again, you create an array with a retain count of +1, then you add it to the myVector array, which will retain it again (+2). However, you no longer have a pointer to the array, so you've leaked it.

Both of these leaks can be solved by using [NSMutableArray arrayWithCapacity:size] instead of the alloc/init approach. This will create an autoreleased array, which will solve your memory leak.

If you don't know what autoreleasing is, then you may want to consider creating the array and storing it into a local variable, adding it to myVector (or setting it into myVector), and then immediately releasing it afterwards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文