Objective-C:在取消分配父对象之前,是否必须释放属性对象? [英] Objective-C: Do you have to dealloc property objects before deallocating the parent object?

查看:159
本文介绍了Objective-C:在取消分配父对象之前,是否必须释放属性对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为foo的对象和另一个名为bar的对象作为属性。



当foo释放时,它是否会自动删除对bar的所有引用,以便bar也可以释放?或将富解除分配和酒吧浮在内存的某个地方?即使所有酒吧的引用都是在foo中定义的。



在此先感谢。

foo 对象有任何保留或副本(谢谢Dave) bar ,例如当你声明属性为以下任何一种时:

  @property(nonatomic,retain)NSString *酒吧; 
//或
@property(非原子,复制)NSString * bar;

当你需要释放 bar 时你取消分配 foo

   - (void)dealloc 
{
[bar release];

[super dealloc];
}

系统不会免费 bar 的内存空间,直到你摆脱所有对它的引用(即引用计数降到0),所以你必须自己监视引用计数和对象。

Let's say I have an object named "foo" with another object named "bar" as property.

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".

thanks in advance.

解决方案

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:

@property (nonatomic, retain) NSString *bar;
// Or
@property (nonatomic, copy) NSString *bar;

You'll need to release bar when you deallocate 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.

这篇关于Objective-C:在取消分配父对象之前,是否必须释放属性对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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