简体   繁体   English

从方法中返回NSMutableArray ...我之前是否发布它? - iPhone SDK

[英]Returning an NSMutableArray from a method…do I release it prior? - iPhone SDK

I have been very confused on how to handle the releasing of an NSMutableArray when I need to return it from a method. 当我需要从方法返回时,我一直很困惑如何处理NSMutableArray的释放。 I am not even sure if I am suppose to release it or not. 我甚至不确定我是否应该释放它。

I have the code: 我有代码:

-(NSArray *)methodToCall {

        NSMutableArray *mutArray = [[NSMutableArray alloc] initWithCapacity:10];

        //Fill the array with stuff here

        [mutArray release];  //Am I suppose to have this to keep the memory down?
        return mutArray;
}

My question is whether or not I am suppose to have the [mutArray release]; 我的问题是我是否想要[mutArray release]; in the code or not. 在代码与否。 Could someone explain this? 有人可以解释一下吗? I am at a loss and I want to keep my code as clean and leak free as possible. 我很茫然,我希望尽可能保持我的代码干净无泄漏。

The caller of your method is going to expect a valid NSArray, but which it doesn't own. 您的方法的调用者将期望有效的NSArray,但它不拥有。

If you release it the way you're doing in your question, you're returning an invalid object (because you've both allocated and released it.) So that's not what you want. 如果你按照你在问题中的方式release它,你就会返回一个无效的对象(因为你已经分配并释放了它。)所以这不是你想要的。

You should "autorelease" the array before returning it. 你应该在返回之前“自动释放”数组。 Then you'll be allocating it, but relinquishing ownership (without forcibly releasing it) before returning it. 然后你将分配它,但在返回之前放弃所有权(不强行释放它)。 It will be valid until the end of the current event loop when the autorelease pool gets cleaned up, and your caller can retain it or let it go as appropriate: 当自动释放池被清理时,它将在当前事件循环结束之前有效,并且您的调用者可以保留它或让它适当地离开:

-(NSArray *)methodToCall {

    NSMutableArray *mutArray = [[NSMutableArray alloc] initWithCapacity:10];

    //Fill the array with stuff here

    return [mutArray autorelease];
}

Your other alternative, since you don't need to keep the reference around either, is to use one of the "autoreleasing" convenience methods to create it in the first place: 您的另一种选择,因为您不需要保留参考,或者使用“自动释放”方便方法之一来创建它:

-(NSArray *)methodToCall {

    // We're not doing an explicit alloc/init here, so...
    NSMutableArray *mutArray = [NSMutableArray arrayWithCapacity:10];

    // ...no autorelease necessary.
    return mutArray;
}

Short answer - No. 简答 - 没有。

As it is now, you are allocating an array and then releasing (freeing) it before the return. 就像现在一样,您正在分配一个数组,然后在返回之前释放(释放)它。 So the when you try accessing the return object from your calling method you're going to get a crash. 因此,当您尝试从调用方法访问返回对象时,您将遇到崩溃。

My suggestion would be to use autorelease or to have the calling method or class ivar be responsible for this array object if it is used often. 我的建议是使用autorelease或者如果经常使用,则调用方法或类ivar负责此数组对象。

An example of the autorelease : autorelease一个例子:

NSMutableArray *mutArray = [[[NSMutableArray alloc] initWithCapacity:10] autorelease];

I also suggest reading the Memory Management from the Developer Documents. 我还建议从开发者文档中阅读内存管理

In general, instead of using alloc/init to create a temporary array, consider using a convenience creation method ( +arrayWithCapacity: in this case): 通常,不要使用alloc / init来创建临时数组,而应考虑使用便捷创建方法(在这种情况下为+arrayWithCapacity:

- (NSArray *)methodToCall
{
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:10];

    // Fill the array with stuff here

    return mutableArray;
}

Convenience creation methods such as +arrayWithCapacity: return an object that the caller is not responsible for, relieving the calling code of the burden of managing memory. 方便创建方法如+arrayWithCapacity:返回调用者不负责的对象,减轻调用代码管理内存的负担。 Since the calling code is simply returning the reference rather than storing it, that'll simplify things. 由于调用代码只是返回引用而不是存储它,这将简化操作。

if you go for explicit object allocation by calling alloc and init you are owner of your object, so you are responsible for its object retain value else you do it by implicit you don't need to care about it. 如果你通过调用alloc和init来进行显式对象分配,那么你就是对象的所有者,所以你要对它的对象保留值负责,否则你通过隐式做它,你不需要关心它。 it will take care of itself. 它会照顾好自己。

-(NSMutableArray *)getArray
{
     NSMutableArray *array=[[NSMutableArray alloc] initWithCapacity:10];

     //your code

    return [array autorelease];
}

in the above code we are the owner of the object so we need to handle its retain count by passing autorelease the autoreleasepool will take care of it. 在上面的代码中,我们是对象的所有者,所以我们需要通过传递autorelease来处理它的保留计数,autoreleasepool会处理它。

-(NSMutableArray *)getArray
{
     NSMutableArray *array=[NSMutableArray allocWithCapacity:10];

     //your code

    return array;
}

in the above code we didn't alloc any object we just call class method to define the size of the array. 在上面的代码中我们没有分配任何对象,我们只是调用类方法来定义数组的大小。

if you want more details go for the object ownership in Memory management guide from apple library 如果您想了解更多详细信息,请参阅Apple库中的内存管理指南中对象所有权

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM