帮助我理解为什么我不必分配这个 NSMutableArray

发布于 2024-11-18 14:42:33 字数 968 浏览 2 评论 0原文

在firstViewController中,

SecondViewController *secondViewController = [[[SecondViewController alloc] init] autorelease];
// Pass data to secondViewController
[secondViewController setClass1:anObjectFromFirstViewController];
[[self navigationController] pushViewController:secondViewController ...];

SecondViewController.m

Class1 *class1;
NSMutableArray *object2;

在viewDidLoad中, 我认为object2指向与class1相同的对象,因此不需要分配。

[self setObject2:[class1 someNSMutableArray]];

在 init 中,我不必分配 object2,整个应用程序仍然可以工作。是因为 object2 指向内存中与 [class1 someNSMutableArray] 相同的项目吗?

如果是这种情况,那么如果我分配 object2 会发生什么。会有2本吗?如果我初始化该应用程序,它仍然可以正常工作。

-(id)init { object2 = [[NSMutableArray alloc] init]; }

令人困惑的部分是,如果我使用 autorelease 进行初始化,它会给我一个 dealloc 错误,即解除分配到已释放对象错误,

-(id)init { object2 = [[[NSMutableArray alloc] init] autorelease]; }

非常感谢!

In firstViewController,

SecondViewController *secondViewController = [[[SecondViewController alloc] init] autorelease];
// Pass data to secondViewController
[secondViewController setClass1:anObjectFromFirstViewController];
[[self navigationController] pushViewController:secondViewController ...];

SecondViewController.m

Class1 *class1;
NSMutableArray *object2;

In viewDidLoad,
I think object2 points to the same object as class1 and therefore does not need to be allocated.

[self setObject2:[class1 someNSMutableArray]];

In init, I don't have to allocate object2 and the whole app still works. Is it because the object2 is pointing to the same item in memory as [class1 someNSMutableArray].

If that is the case, then what happens if I do allocate object2. Will there be 2 copies? The app still works the same if I initialize it.

-(id)init { object2 = [[NSMutableArray alloc] init]; }

The confusing part is that if I initialize with autorelease, it will give me a dealloc error, the deallocating to a released object error

-(id)init { object2 = [[[NSMutableArray alloc] init] autorelease]; }

Thanks mucho!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我的鱼塘能养鲲 2024-11-25 14:42:33

您对问题第一部分的想法是正确的。

[self setObject2:[class1 someNSMutableArray]];

object2 是一个变量,现在包含与 [class1 someNSMutableArray] 相同的地址。所以两者都指向同一个 NSMutableArray 对象。

这与以下情况不同:

object2 = [[NSMutableArray alloc] init];

在本例中,您创建一个新的 NSMutableArray 对象并将地址放入 object2 实例变量中。所以现在它指向一个与 [class1 someNSMutableArray] 不同的数组。

这里的关键词是“创造”。将 alloc/init 视为创建一个新对象。

至于内存管理:

object2 = [[[NSMutableArray alloc] init] autorelease];

我假设您(正确地)在此类的 -dealloc 中释放了 object2 。因此,您有两个释放(dealloc 中的释放加上此处的自动释放),而只有一个保留(来自 alloc/init),因此您过度释放了该对象。

Your idea on the first part of the question is correct.

[self setObject2:[class1 someNSMutableArray]];

object2 is a variable which now contains the same address as [class1 someNSMutableArray]. So both point to the same NSMutableArray object.

This is different from:

object2 = [[NSMutableArray alloc] init];

In this case you have created a new NSMutableArray object and put that address in the object2 instance variable. So that is now pointing to a different array than [class1 someNSMutableArray].

The key word there is "created". Think of alloc/init as creating a new object.

As for memory management:

object2 = [[[NSMutableArray alloc] init] autorelease];

I'm assuming you are (correctly) releasing object2 in the -dealloc for this class. So you have two releases (that release in dealloc plus this autorelease here) against only one retain (from the alloc/init), and therefore you are over-releasing the object.

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