Objective C版本,自动释放和数据类型 [英] Objective C release, autorelease, and data types

查看:100
本文介绍了Objective C版本,自动释放和数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是内存托管代码的新手,但我很清楚这个想法。

I'm new to memory managed code but I get the idea pretty well.

通过XCode中的泄漏工具获取我的应用程序时,我注意到我只有清理我的自定义对象,但不是动态创建的数组,所以我认为这些数据类型是自动释放的 - 这是有意义的,因为我只需要释放我用作具有(保留)它们的属性的数组。

On taking my app through the leaks tool in XCode, I noticed I only had to clean up my custom objects, but not dynamically created arrays for example, so I figured those data types are autoreleased - makes sense since I only had to release the arrays I used as properties that had a (retain) on them.

然后我注意到一些奇怪的事情:我在某个初始化的数组上得到了泄漏:

Then I noticed something peculiar : I was getting a leak on a certain array initialized like this :

NSMutableArray *removals = [NSMutableArray new];

但不是类似的

NSMutableArray *removals = [NSMutableArray arrayWithCapacity:9];

现在,设置new的原因是它可能有0-99个项目在其中,而我知道的另一个将永远是9.因为两个数组后来基于用户交互传递给相同的方法,如果我没有在方法结束时释放,或者我要么泄漏,或者如果我这样做是个例外!

Now, the reason one was set up with "new" is that it could have 0-99 items in it, whereas the other one I knew was going to always be 9. Since both arrays are passed to the same method later based on user interaction, I was either getting a leak if I did not release at the end of the method, or an exception if I did!

我将第一个数组更改为

NSMutableArray *removals = [NSMutableArray arrayWithCapacity:99];

我没有泄漏,也没有必要发布任何东西。任何人都可以解释一下吗?

and I get no leaks and don't have to release anything. Can anyone explain?

推荐答案

内存管理规则,只要你有一个用 + alloc + new -copy -mutableCopy ,你拥有它并负责在某个时候发布它。 (事实上​​, + new 只是 [[MyClass alloc] init] 。)如你所知,通过 [NSArray new] 创建一个数组而不释放它是一个内存泄漏。但是,如果正确处理此对象,通常可以在某个时候释放它。例如:

As noted in the memory management rules, whenever you have an object that you have created with +alloc, +new, -copy, or -mutableCopy, you own it and are responsible for releasing it at some point. (In fact, +new is just shorthand for [[MyClass alloc] init].) As you noted, creating an array via [NSArray new] without releasing it is a memory leak. However, if you handle this object properly, it is usually possible to release it at some point. For example:


  • 如果使用的方法是从创建数组的方法,然后你应该能够在使用它之后释放它。如果内部方法需要对数组保持更永久的引用,那么该方法负责发送 -retain ,最后, -release 到对象。例如:

  • If the method that uses the array is called from within the method that creates the array, then you should be able to release the array after it has been used. If the inner method needs to keep a more permanent reference to the array around, then that method is responsible for sending -retain and, eventually, -release to the object. For example:

- (void)myMethod {
    NSArray *removals = [NSArray new];
    // ...
    [someObject someOtherMethod:removals];
    [removals release];
}


  • 如果您在 <$ c中创建了数组对象的$ c> -init 方法,然后 -dealloc 方法可以在释放时释放它对象被销毁。

  • If you created the array in an -init method for an object, then the -dealloc method can release it when the object is destroyed.

    如果你需要创建数组,然后从方法中返回,你就发现了发明自动释放的原因。您的方法的调用者不负责释放该对象,因为它不是 + alloc + new -copy -mutableCopy 方法,但您需要确保它最终发布。在这种情况下,您在返回对象之前手动调用 -autorelease 。例如:

    If you need to create the array and then return it from the method, you've discovered the reason that autoreleasing was invented. The caller of your method isn't responsible for releasing the object, since it isn't an +alloc, +new, -copy, or -mutableCopy method, but you need to ensure it is released eventually. In this case, you manually call -autorelease on the object before you return it. For example:

    - (NSArray *)myMethod {
        NSArray *removals = [NSArray new];
        // ...
        return [removals autorelease];
    }
    


  • 当你创建数组通过 + arrayWithCapacity:,您没有调用其中一个特殊方法,因此您不必释放结果。这可能是用 -autorelease 实现的,就像上面的上一个例子,但不一定如此。 (顺便提一下,您还可以使用 [NSMutableArray array] 创建一个空的自动释放的NSMutableArray;该方法可以在NSArray中找到,因此它不会显示在NSMutableArray下的文档中,但是当它被发送到NSMutableArray类时它将创建一个可变数组。)如果你要从你的方法返回数组,你可以用它作为 [[[NSMutableArray alloc] init]的简写autorelease] - 但它只是一个捷径。但是,在许多情况下,您可以使用 -init + new 创建一个对象,并在适当的位置手动释放它。时间。

    When you create the array via +arrayWithCapacity:, you aren't calling one of the "special" methods, so you do not have to release the result. This is probably implemented with -autorelease, much like the last example above, but not necessarily. (Incidentally, you can also create an empty autoreleased NSMutableArray with [NSMutableArray array]; the method is found in NSArray, so it won't show up in the documentation under NSMutableArray, but it will create a mutable array when sent to the NSMutableArray class.) If you're going to be returning the array from your method, you can use this as shorthand for [[[NSMutableArray alloc] init] autorelease]—but it is just a shortcut. In many situations, though, you can create an object with -init or +new and manually release it at the appropriate time.

    这篇关于Objective C版本,自动释放和数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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