Let's say I have an object named "foo" with another object named "bar" as property.
假设我有一个名为“foo”的对象,另一个名为“bar”的对象作为属性。
When "foo" deallocates, will it automatically remove all references to "bar" so that "bar" deallocates as well? or will "foo" deallocate and "bar" float in memory somewhere? even if all of "bar"'s references are defined in "foo".
当“foo”解除分配时,它会自动删除对“bar”的所有引用,以便“bar”也可以解除分配吗?或者“foo”deallocate和“bar”漂浮在某个地方的记忆中?即使所有“bar”的引用都在“foo”中定义。
thanks in advance.
提前致谢。
4 个解决方案
#1
15
If the foo
object has any retains on or copies of (thanks Dave) bar
, for example when you declare the property as either one of these:
如果foo对象有任何保留或(感谢Dave)栏的副本,例如当您将属性声明为以下任何一个时:
@property (nonatomic, retain) NSString *bar;
// Or
@property (nonatomic, copy) NSString *bar;
You'll need to release bar
when you deallocate foo
:
当你解除分配foo时,你需要释放吧:
- (void)dealloc
{
[bar release];
[super dealloc];
}
The system won't free bar
's memory space for you until you get rid of all references to it (i.e. reference count goes down to 0), so you'll have to monitor your reference counts and objects yourself.
系统不会释放条形码的存储空间,直到你摆脱对它的所有引用(即引用计数降至0),因此你必须自己监视引用计数和对象。
#2
2
If you allocate memory you have to release it. So, yes, put a call to [bar release]
or self.bar = nil
(if you're using synthesized properties and all that) in your dealloc
.
如果你分配内存,你必须释放它。所以,是的,在你的dealloc中调用[bar release]或self.bar = nil(如果你正在使用合成属性和所有这些)。
See here for an intro to memory management on iOS.
请参阅此处了解iOS上的内存管理简介。
#3
0
Object A is responsible for releasing any references to other objects (Object B, Object C, etc) when it is deallocated-- this doesn't happen automatically.
对象A负责在解除分配时释放对其他对象(对象B,对象C等)的任何引用 - 这不会自动发生。
This is done in the -dealloc
method on the object:
这是在对象的-dealloc方法中完成的:
- (void)dealloc
{
[propertyB release];
[propertyC release];
[super dealloc];
}
(or if the properties are read/write and maked as retain
, you can substitute [self setPropertyB:nil]
, etc).
(或者如果属性是读/写并且保留为保留,则可以替换[self setPropertyB:nil]等)。
So what will happen is that when all references to Object A go away, it's deallocated, in turn reducing the reference count on properties B and C. If those objects are only owned by Object A, they, too, will end up being deallocated as a result.
因此,当对对象A的所有引用都消失时,它将被释放,从而减少属性B和C上的引用计数。如果这些对象仅由对象A拥有,它们也将最终被释放为结果。
(This is true of all iPhone OS development which you've tagged. I assume you're not talking about the garbage-collected environment on the Mac, which has different rules and behavior and does do some things automatically.)
(对于您标记的所有iPhone OS开发都是如此。我假设您不是在谈论Mac上的垃圾收集环境,它具有不同的规则和行为,并且会自动执行某些操作。)
#4
0
The big reason to be using self.bar = nil
would be if bar were a reference to a view that was created within a nib file. In that case, one would include that line in -(void)viewDidUnload
, as this will let the system release that object when the view is shuffled out. If the view comes back it will be reloaded through the nib file. This does not, however, obviate one from needing to use 'self.bar = nil
or [bar release]
in -(void) dealloc
使用self.bar = nil的最大原因是,如果bar是对在nib文件中创建的视图的引用。在这种情况下,可以在 - (void)viewDidUnload中包含该行,因为这将使系统在视图被移出时释放该对象。如果视图返回,它将通过nib文件重新加载。但是,这并不能避免需要在 - (void)dealloc中使用'self.bar = nil或[bar release]
#1
15
If the foo
object has any retains on or copies of (thanks Dave) bar
, for example when you declare the property as either one of these:
如果foo对象有任何保留或(感谢Dave)栏的副本,例如当您将属性声明为以下任何一个时:
@property (nonatomic, retain) NSString *bar;
// Or
@property (nonatomic, copy) NSString *bar;
You'll need to release bar
when you deallocate foo
:
当你解除分配foo时,你需要释放吧:
- (void)dealloc
{
[bar release];
[super dealloc];
}
The system won't free bar
's memory space for you until you get rid of all references to it (i.e. reference count goes down to 0), so you'll have to monitor your reference counts and objects yourself.
系统不会释放条形码的存储空间,直到你摆脱对它的所有引用(即引用计数降至0),因此你必须自己监视引用计数和对象。
#2
2
If you allocate memory you have to release it. So, yes, put a call to [bar release]
or self.bar = nil
(if you're using synthesized properties and all that) in your dealloc
.
如果你分配内存,你必须释放它。所以,是的,在你的dealloc中调用[bar release]或self.bar = nil(如果你正在使用合成属性和所有这些)。
See here for an intro to memory management on iOS.
请参阅此处了解iOS上的内存管理简介。
#3
0
Object A is responsible for releasing any references to other objects (Object B, Object C, etc) when it is deallocated-- this doesn't happen automatically.
对象A负责在解除分配时释放对其他对象(对象B,对象C等)的任何引用 - 这不会自动发生。
This is done in the -dealloc
method on the object:
这是在对象的-dealloc方法中完成的:
- (void)dealloc
{
[propertyB release];
[propertyC release];
[super dealloc];
}
(or if the properties are read/write and maked as retain
, you can substitute [self setPropertyB:nil]
, etc).
(或者如果属性是读/写并且保留为保留,则可以替换[self setPropertyB:nil]等)。
So what will happen is that when all references to Object A go away, it's deallocated, in turn reducing the reference count on properties B and C. If those objects are only owned by Object A, they, too, will end up being deallocated as a result.
因此,当对对象A的所有引用都消失时,它将被释放,从而减少属性B和C上的引用计数。如果这些对象仅由对象A拥有,它们也将最终被释放为结果。
(This is true of all iPhone OS development which you've tagged. I assume you're not talking about the garbage-collected environment on the Mac, which has different rules and behavior and does do some things automatically.)
(对于您标记的所有iPhone OS开发都是如此。我假设您不是在谈论Mac上的垃圾收集环境,它具有不同的规则和行为,并且会自动执行某些操作。)
#4
0
The big reason to be using self.bar = nil
would be if bar were a reference to a view that was created within a nib file. In that case, one would include that line in -(void)viewDidUnload
, as this will let the system release that object when the view is shuffled out. If the view comes back it will be reloaded through the nib file. This does not, however, obviate one from needing to use 'self.bar = nil
or [bar release]
in -(void) dealloc
使用self.bar = nil的最大原因是,如果bar是对在nib文件中创建的视图的引用。在这种情况下,可以在 - (void)viewDidUnload中包含该行,因为这将使系统在视图被移出时释放该对象。如果视图返回,它将通过nib文件重新加载。但是,这并不能避免需要在 - (void)dealloc中使用'self.bar = nil或[bar release]