自动释放的 NSMutableArray 未填充

发布于 2024-11-07 00:44:07 字数 1794 浏览 0 评论 0原文

我想填充这样的数组:

NSMutableArray *array = [self methodThatReturnsAnArray];

在“methodThatReturnsAnArray”方法中,我创建一个这样的数组:

NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];

当我完成填充“arrayInMethod”时,我将返回该数组,为了防止内存泄漏,我正在使用:

return [arrayInMethod autorelease];

但是“数组”变量永远不会被填充。当删除“自动释放”时,它工作正常。我应该怎么做才能确保我释放了返回的对象?

编辑

+ (NSMutableArray *)buildInstants:(NSArray *)huntsArray {

    NSMutableArray *goGetObjects = [[[NSMutableArray alloc] init] autorelease];

    for (int i = 0; i < [huntsArray count]; i++) {

        NSDictionary *huntDict = [huntsArray objectAtIndex:i];

        PHGoGet *goGet = [[PHGoGet alloc] init];

        goGet.title = [huntDict objectForKey:@"title"];
        goGet.description = [huntDict objectForKey:@"description"];
        goGet.start = [huntDict objectForKey:@"start"];
        goGet.end = [huntDict objectForKey:@"end"];
        goGet.ident = [huntDict objectForKey:@"id"];

        if ((CFNullRef)[huntDict objectForKey:@"image_url"] != kCFNull) {

            goGet.imageURL = [huntDict objectForKey:@"image_url"];

        } else {

            goGet.imageURL = nil;
        }

        if ((CFNullRef)[huntDict objectForKey:@"icon_url"] != kCFNull) {

            goGet.iconURL = [huntDict objectForKey:@"icon_url"];

        } else {

            goGet.iconURL = nil;
        }

        goGet.longitude = [huntDict objectForKey:@"lng"];
        goGet.latitude = [huntDict objectForKey:@"lat"];
        goGet.companyIdent = [huntDict objectForKey:@"company_id"];

        [goGetObjects insertObject:goGet atIndex:i];
        [goGet release];
    }

    return [[goGetObjects copy] autorelease];
}

I want to populate an array like this:

NSMutableArray *array = [self methodThatReturnsAnArray];

In the "methodThatReturnsAnArray"-method I create an array like this:

NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];

When I'm finished populating "arrayInMethod" I'm returning the array and in order to prevent a memory leak I'm using:

return [arrayInMethod autorelease];

However the "array"-variable is never populated. When removing the "autorelease" it works fine though. What should I do in order to make sure that the returned object i released?

EDIT

+ (NSMutableArray *)buildInstants:(NSArray *)huntsArray {

    NSMutableArray *goGetObjects = [[[NSMutableArray alloc] init] autorelease];

    for (int i = 0; i < [huntsArray count]; i++) {

        NSDictionary *huntDict = [huntsArray objectAtIndex:i];

        PHGoGet *goGet = [[PHGoGet alloc] init];

        goGet.title = [huntDict objectForKey:@"title"];
        goGet.description = [huntDict objectForKey:@"description"];
        goGet.start = [huntDict objectForKey:@"start"];
        goGet.end = [huntDict objectForKey:@"end"];
        goGet.ident = [huntDict objectForKey:@"id"];

        if ((CFNullRef)[huntDict objectForKey:@"image_url"] != kCFNull) {

            goGet.imageURL = [huntDict objectForKey:@"image_url"];

        } else {

            goGet.imageURL = nil;
        }

        if ((CFNullRef)[huntDict objectForKey:@"icon_url"] != kCFNull) {

            goGet.iconURL = [huntDict objectForKey:@"icon_url"];

        } else {

            goGet.iconURL = nil;
        }

        goGet.longitude = [huntDict objectForKey:@"lng"];
        goGet.latitude = [huntDict objectForKey:@"lat"];
        goGet.companyIdent = [huntDict objectForKey:@"company_id"];

        [goGetObjects insertObject:goGet atIndex:i];
        [goGet release];
    }

    return [[goGetObjects copy] autorelease];
}

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

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

发布评论

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

评论(2

原来分手还会想你 2024-11-14 00:44:07

尝试使用 NSMutableArray 的便捷方法...更改:

NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];

To...

NSMutableArray *arrayInMethod = [NSMutableArray array];

array 将返回一个自动释放的对象。

Try using the convienence method for NSMutableArray... change:

NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];

To...

NSMutableArray *arrayInMethod = [NSMutableArray array];

array will return an autoreleased object.

本宫微胖 2024-11-14 00:44:07

首先,我建议您不要从任何方法返回 NSMutableArray。最好使用 NSArray 来避免一些非常难以调试的问题。我的建议是:

声明可变数组并填充它:

NSMutableArray *arrayInMethod = [[[NSMutableArray alloc] init] autorelease];

然后返回一个自动释放的副本:

return [[arrayInMethod copy] autorelease];

最后,当您获取返回的数组时,再次使其可变(仅当您需要更改它时):

NSMutableArray *array = [[self methodThatReturnsAnArray] mutableCopy];

完成后使用数组,您可以释放它:

[array release];

First of all, I recommend you not to return a NSMutableArray from any method. It's better to use NSArray for that to avoid some very difficult to debug problems. My proposition is:

You declare the mutable array and populate it:

NSMutableArray *arrayInMethod = [[[NSMutableArray alloc] init] autorelease];

Then you return an autoreleased copy:

return [[arrayInMethod copy] autorelease];

And finally, when you take the returned array, you make it mutable again (only if you need to change it):

NSMutableArray *array = [[self methodThatReturnsAnArray] mutableCopy];

When you're done with the array, you release it:

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