将对象设置为nil对release + realloc [英] Setting an object nil versus release+realloc

查看:130
本文介绍了将对象设置为nil对release + realloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是垃圾收集环境



我有一个类的实例变量,在我的运行时的某一点,我需要重新初始化一个不同的数据集



假设我有一个 NSMutableArray NSMutableDictionary ,会更有效地执行以下操作:

  ; 
myArr = [[NSMutableArray alloc] init ....];

或只是,

  myArr = nil;将$ myArr释放对象,让我没有指向内存中的存储器的指针,所以我可以重新创建一个新的对象,使用myArr?

解决方案

如果你自己执行 myArr = nil; ,那么您丢失了可以将发布消息发送到的指针。



没有魔法可以 ,该记忆已泄漏。


This is not a garbage collected environment

I have a class instance variable which at some point in my runtime, I need to re-initialize with a different data set than it was originally constructed with.

Hypothetically speaking, what if I have an NSMutableArray or an NSMutableDictionary, would it be more efficient to do something such as:

[myArr release];
myArr  = [[NSMutableArray alloc] init....];

or just,

myArr = nil;

Will myArr release the object and leave me without a pointer to the storage in memory so I can re-use myArr?

解决方案

If you do myArr=nil; by itself, then you've lost the pointer to which you can send the release message to. There is no magic to release your object.

And, as Georg says, without being able to release your object, that memory has 'leaked'.

这篇关于将对象设置为nil对release + realloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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