内存管理分配新对象

时间:2022-04-19 07:18:56

Suppose I have instance variable where I defined as retained in the header file. I assign an object to it when the class is initialized. Now suppose in some function I assign a new object to my instance variable. Do I need to release the previous retained object?

假设我有一个实例变量,我将其定义为保留在头文件中。我在初始化类时为它指定了一个对象。现在假设在某个函数中我将一个新对象分配给我的实例变量。我是否需要释放以前保留的对象?

4 个解决方案

#1


0  

No, the auto generated retain setter will call release if necessary, as long as you are using the setter (either directly or with dot notation). The setter would look something like this:

不,只要您使用设置器(直接或带点表示法),自动生成的保留设置器将在必要时调用释放。 setter看起来像这样:

-(void)setObject:(id)newObject{
    if(newObject != myCurrentObject){
       [myCurrentObject release];
       myCurrentObject = [newObject retain];
    }
}

See Apple's Memory Management Programming Guide - Section Accessor Methods.

请参阅Apple的内存管理编程指南 - 部分访问器方法。

#2


5  

Depends on how you assign it. If you assign it directly to the member variable, then yes, you would need to release the old one. If you assign it using the public property, e.g. self.propertyname then the old one will be released automatically.

取决于你如何分配它。如果直接将其分配给成员变量,那么是的,您需要释放旧的变量。如果您使用公共属性进行分配,例如self.propertyname然后旧的将自动释放。

#3


1  

If you allocate a new object, and you want to assign it to a object which is already "owned", then you must release that object and gain "ownership" of the new one.

如果您分配了一个新对象,并且想要将其分配给已经“拥有”的对象,那么您必须释放该对象并获得新对象的“所有权”。

#4


0  

No, the generated setter method will release/autorelease the value previously reference by the ivar for the property.

不,生成的setter方法将释放/自动释放ivar先前为属性引用的值。

Apple's Memory Management docs, although lengthy, are well worth a read for a better understanding.

Apple的内存管理文档虽然冗长,但值得阅读以便更好地理解。

Apple's Memory Management Documentation

Apple的内存管理文档

#1


0  

No, the auto generated retain setter will call release if necessary, as long as you are using the setter (either directly or with dot notation). The setter would look something like this:

不,只要您使用设置器(直接或带点表示法),自动生成的保留设置器将在必要时调用释放。 setter看起来像这样:

-(void)setObject:(id)newObject{
    if(newObject != myCurrentObject){
       [myCurrentObject release];
       myCurrentObject = [newObject retain];
    }
}

See Apple's Memory Management Programming Guide - Section Accessor Methods.

请参阅Apple的内存管理编程指南 - 部分访问器方法。

#2


5  

Depends on how you assign it. If you assign it directly to the member variable, then yes, you would need to release the old one. If you assign it using the public property, e.g. self.propertyname then the old one will be released automatically.

取决于你如何分配它。如果直接将其分配给成员变量,那么是的,您需要释放旧的变量。如果您使用公共属性进行分配,例如self.propertyname然后旧的将自动释放。

#3


1  

If you allocate a new object, and you want to assign it to a object which is already "owned", then you must release that object and gain "ownership" of the new one.

如果您分配了一个新对象,并且想要将其分配给已经“拥有”的对象,那么您必须释放该对象并获得新对象的“所有权”。

#4


0  

No, the generated setter method will release/autorelease the value previously reference by the ivar for the property.

不,生成的setter方法将释放/自动释放ivar先前为属性引用的值。

Apple's Memory Management docs, although lengthy, are well worth a read for a better understanding.

Apple的内存管理文档虽然冗长,但值得阅读以便更好地理解。

Apple's Memory Management Documentation

Apple的内存管理文档