将对象设置为nil对release + realloc [英] Setting an object nil versus 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屋!