NSMutableArrays - 我可以这样做吗?
在我的应用程序中,单例类(SharedData)为 NSMutableArray 分配内存:
[self sharedMutableArray] = [[NSMutableArray alloc] init];
A 类填充此共享MutableArray:
NSObject *obj = [NSObject alloc] init];
[sharedMutableArray addObject];
obj = nil;
B 类执行此操作 - 这就是我的问题:
NSMutableArray *tmpArray = sharedMutableArray;
... uses the tmpArray locally
[tmpArray removeAllObjects];
tmpArray = nil;
这是一个继承的代码,我的预感是这是一个 NO-NO。有人可以确认将 nil 分配给 tmpArray 也会释放共享MutableArray 的内存吗...我猜作者只想释放 tmpArray...
In my app, the singleton class (SharedData) allocates memory for a NSMutableArray:
[self sharedMutableArray] = [[NSMutableArray alloc] init];
Class A populates the this sharedMutableArray:
NSObject *obj = [NSObject alloc] init];
[sharedMutableArray addObject];
obj = nil;
Class B does this - and that's my question:
NSMutableArray *tmpArray = sharedMutableArray;
... uses the tmpArray locally
[tmpArray removeAllObjects];
tmpArray = nil;
This is an inherited code and my hunch is that this is a NO-NO. Can some one confirm that assigning nil to tmpArray will release memory for sharedMutableArray also.... I guess the author wanted to release tmpArray only...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
nil
分配给 tmpArray 只会将指向该对象的指针设置为nil
。它根本不影响对象本身(或其生命周期)。在这种情况下,将您创建的对象设置为 nil 不会执行任何操作,因为它们的变量声明位于本地范围内 - 如果您希望从内存中释放对象,您需要将它们发送给它们在将指向对象的指针设置为nil
之前释放。但是,发送
removeAllObjects
会影响您的原始共享数组,因为您没有复制该数组,您只是设置了一个新指针来指向“单例”。您可能想要这样:在上述情况下您不需要使用
removeAllObjects
因为它将自动释放。我建议您阅读此。Assigning
nil
to tmpArray only sets your pointer to the object tonil
. It does not affect the object itself (or its lifecycle) at all. In this case, setting the objects you've created tonil
does nothing, since their variable declaration is in local scope - if you want the objects to be deallocated from memory you need to send themrelease
before setting the pointer to the object tonil
.However, sending
removeAllObjects
is affecting your original sharedArray, because you didn't copy the array, you simply set a new pointer to point to the 'singleton'. You probably want this:You won't need to use
removeAllObjects
in the above case because it will be autorelease'd. I suggest you read this.tmpArray 是一个指针,它被初始化为指向sharedMutableArray 指向的同一个可变数组。因此,行:
将清空数组,任何使用sharedMutableArray的人都会看到这一变化。换句话说,赋值
不会复制数组本身,它只复制指针。使用该指针发送的任何消息都将发送到共享数组。同样,将
nil
分配给tmpArray
会设置指针tmpArray
,但不会对数组本身执行任何操作。最后,将变量设置为
nil
永远不会释放内存。另一方面,将属性设置为 nil 会在某些条件下释放内存(例如,当声明该属性保留其内容时)。您在此处设置一个变量,而不是一个属性,因此该数组不可能被释放。tmpArray
is a pointer, and it's initialized to point to the same mutable array that sharedMutableArray points to. For that reason, the line:will empty out the array, and anyone using
sharedMutableArray
will see that change. In other words, the assignmentdoesn't make a copy of the array itself, it only copies the pointer. Any messages you send using that pointer will go to the shared array. Likewise, assigning
nil
totmpArray
sets the pointertmpArray
, but doesn't do anything to the array itself.Finally, setting a variable to
nil
never releases memory. Setting a property to nil, on the other hand, will release memory under some conditions (e.g. when the property is declared to retain its contents). You're setting a variable here, not a property, so there's no chance that the array will be released.