释放/自动释放混乱在可可为iphone [英] release/autorelease confusion in cocoa for iphone

查看:146
本文介绍了释放/自动释放混乱在可可为iphone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在慢慢地为iPhone讲解可可(通过 Stanford Class在iTunes U上)和我刚刚经历了内存管理的一部分,我希望得到一些确认,我对如何处理内存的假设,如何[发布]和[ autorelease]工作。因为内存管理是一个真正基本的,基础的,但是非常重要的编程经验的一部分,我想确保我做的正确。



我理解需要释放具有alloc,new或copy的任何内容。

如果我这样做:

  NSString * temp = [[NSString alloc] initWithString:@Hello World]; 

然后我需要添加[temp release / autorelease]但是,如果我这样做:

  NSString * temp = @你好,世界; 

然后它似乎不需要发布语句。 NSString类是否自动调用autorelease作为赋值的一部分?



此外,这两个语句之后的两个* temp对象之间有什么区别吗?它们都包含相同的字符串,但是有不同的内存/使用方式吗?



其次,使用属性,我假设自动释放是自动处理的。如果我有这个:

  @interface Person:NSObject 
{
// ivars
NSString * firstName;
NSString * lastName;
}

//属性
@property NSString * firstName;
@property NSString * lastName;

///下一个文件

@implementation人

@synthesize firstName;
@synthesize lastName;

- (void)dealloc
{

//这里!

[super dealloc];
}



我假设我不需要添加[firstName发布] [lastName release](at // HERE !!!!),因为那是由属性自动处理的。这是正确的吗?



我明白,如果我这样做的代码(假设我已经定义initWithFirstName):

  Person * Me = [[Person alloc] initWithFirstName:@Drew,lastName:McGhie 

以后我必须使用[Me release / autorelease];



任何帮助确认或纠正我的理解迄今非常感谢。


POST ANSWER WRITE UP


我以为我写完所有的答案,并测试出的建议,并谈论什么工作后,所有的。



我需要添加[firstName发布],[lastName发布],但我还需要添加(保留)到属性描述。不添加(retain)引起的警告,因为它假设(assign)。下面是我最终设置类的方法

  @interface Person:NSObject 
{
// ivars
NSString * firstName;
NSString * lastName;
}

//属性
@property(retain)NSString * firstName;
@property(retain)NSString * lastName;

///下一个文件

@implementation人

@synthesize firstName;
@synthesize lastName;

- (void)dealloc
{
[firstName release];
[lastName release];
[super dealloc];
}


解决方案

规则很简单:if您 alloc copy retain release 。如果你没有,它不是。但是,如果你需要依赖一个对象,你必须 retain (然后 release )。 / p>

我们可以根据规则处理字符串文字 - 你不需要 release 拥有它。这很简单;没有必要担心他们是否是特殊情况,只是遵守规则,你会很好。



我写了一个博客文章集合关于Cocoa内存管理规则的文章;我建议您跟进一些参考资料。


I'm slowly teaching myself cocoa for the iPhone(through the Stanford Class on iTunes U) and I've just gone through the part on memory management, and I wanted to hopefully get some confirmation that the assumptions I'm making on how memory is handled and how [release] and [autorelease] work. Since memory management is a really basic and fundamental, but very essential part of the programming experience, I'd like to make sure I'm doing it right.

I understand that anything with an alloc, new, or copy needs to be released.
If I do this:

NSString *temp = [[NSString alloc] initWithString:@"Hello World"];

Then I need to add [temp release/autorelease] eventually, since I have an alloc.

However, if I do this:

NSString *temp = @"Hello World";

Then it doesn't seem to need a release statement. Does the NSString class call autorelease automatically as part of the assignment?

Also, is there any difference between the two *temp objects here after these statements? They both contain the same string, but are there memory/usage ways where they differ?

Secondly, with properties, I'm assuming that the autorelease is handled automatically. If I have this:

@interface Person : NSObject
{
    //ivars
    NSString *firstName;
    NSString *lastName;
}

//properties
@property NSString *firstName;
@property NSString *lastName;

///next file

@implementation Person

@synthesize firstName;
@synthesize lastName;

- (void) dealloc
{

    //HERE!!!!

    [super dealloc];
}

I'm assuming I don't need to add [firstName release] and [lastName release] (at //HERE!!!!), since that's automatically handled by the properties. Is that correct?

I do understand that if I do this in code(assuming I've defined initWithFirstName):

Person *Me = [[Person alloc] initWithFirstName: @"Drew", lastName:"McGhie"];

that later I'm going to have to use [Me release/autorelease];

Any help confirming or correcting my understanding so far is greatly appreciated.

POST ANSWER WRITE-UP

I thought I'd write this all up after going over all the answers and testing out the suggestions and talk about what worked.

I do need to add the [firstName release], [lastName release], but I also need to add (retain) to the property descriptions. Not adding the (retain) caused warnings because it assumes (assign). Here's how I finally set up the class

@interface Person : NSObject
    {
        //ivars
        NSString *firstName;
        NSString *lastName;
    }

    //properties
    @property (retain) NSString *firstName;
    @property (retain) NSString *lastName;

    ///next file

    @implementation Person

    @synthesize firstName;
    @synthesize lastName;

    - (void) dealloc
    {
        [firstName release];
        [lastName release];
        [super dealloc];
    }

解决方案

The rule is simple: if you alloc, copy or retain, it's your responsibility to release. If you didn't, it's not. However, if you need to rely on an object staying around, you have to retain (and subsequently release).

We can treat the string literal according to the rules - you don't need to release it because you don't own it. That's simple; there's no need to worry about whether they're special cases or not, just follow the rules and you'll be fine.

I wrote up a blog post with a collection of articles about the Cocoa memory management rules; I'd recommend following up some of the references.

这篇关于释放/自动释放混乱在可可为iphone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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