NSMutableArrays - 我可以这样做吗?

发布于 2024-12-18 15:49:50 字数 569 浏览 1 评论 0原文

在我的应用程序中,单例类(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狠疯拽 2024-12-25 15:49:50

nil 分配给 tmpArray 只会将指向该对象的指针设置为 nil。它根本不影响对象本身(或其生命周期)。在这种情况下,将您创建的对象设置为 nil 不会执行任何操作,因为它们的变量声明位于本地范围内 - 如果您希望从内存中释放对象,您需要将它们发送给它们在将指向对象的指针设置为nil之前释放。

但是,发送 removeAllObjects 会影响您的原始共享数组,因为您没有复制该数组,您只是设置了一个新指针来指向“单例”。您可能想要这样:

NSMutableArray *tmpArray = [NSMutableArray arrayWithArray:sharedMutableArray];

在上述情况下您不需要使用 removeAllObjects 因为它将自动释放。我建议您阅读

Assigning nil to tmpArray only sets your pointer to the object to nil. It does not affect the object itself (or its lifecycle) at all. In this case, setting the objects you've created to nil does nothing, since their variable declaration is in local scope - if you want the objects to be deallocated from memory you need to send them release before setting the pointer to the object to nil.

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:

NSMutableArray *tmpArray = [NSMutableArray arrayWithArray:sharedMutableArray];

You won't need to use removeAllObjects in the above case because it will be autorelease'd. I suggest you read this.

一场春暖 2024-12-25 15:49:50

tmpArray 是一个指针,它被初始化为指向sharedMutableArray 指向的同一个可变数组。因此,行:

[tmpArray removeAllObjects];

将清空数组,任何使用sharedMutableArray的人都会看到这一变化。换句话说,赋值

NSMutableArray *tmpArray = 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:

[tmpArray removeAllObjects];

will empty out the array, and anyone using sharedMutableArray will see that change. In other words, the assignment

NSMutableArray *tmpArray = sharedMutableArray;

doesn'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 to tmpArray sets the pointer tmpArray, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文