iPhone:返回NSMutableArray的方法中,同时还释放 [英] iPhone: Return NSMutableArray in method while still releasing

查看:156
本文介绍了iPhone:返回NSMutableArray的方法中,同时还释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解多一点的内存管理。知道我需要释放什么,我已经init或alloc'ed我感到困惑如下:

I am trying to understand a little more about memory management. Knowing that I need to release anything that I have init or alloc'ed I am confused about the following:

- (NSMutableArray *)getData {
    NSMutableArray *data = [[NSMutableArray alloc] init];
    NSString *first = @"First object";
    [data addObject:first];
    NSString *second = @"Second object";
    [data addObject:second];

    return data;

}

由于我使用的alloc和init,我知道我需要释放我的数据对象。但是,如果我加入自动释放到init部分或回报,它崩溃,当我运行的方法。

Since I used alloc and init, I know I need to release my data object. But if I add autorelease to the init part or to the return, it crashes when I run the method.

什么是做这样的事情以正确的内存管理iPhone的正确方法是什么?

What is the correct way to do something like this with correct memory management for iPhone?

推荐答案

您应该自动释放,就像你说的。来电者可能需要保留,这样的 - 特别是如果存储到一个实例变量:

You should autorelease, like you said. The caller probably needs to retain, like this - especially if storing into an instance variable:

NSMutableArray *array = [[obj getData] retain];

后来,当它完全用它做:

Later, when it's totally done with it:

[array release]; // balances the retain above

这篇关于iPhone:返回NSMutableArray的方法中,同时还释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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