指针/内存管理的可可策略 [英] Cocoa strategy for pointer/memory management

查看:110
本文介绍了指针/内存管理的可可策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多代码,特别是在Apple示例代码中,类似于以下内容:

I see a lot of code, particularly in Apple example code, that resembles the following:

   EditingViewController *controller = [[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil];
    self.editingViewController = controller;
    [controller release];

特别是有任何理由证明上述方法对于:

Is there any reason in particular that the approach above proves beneficial over:

self.editingViewController = [[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil];

尝试了解是否有上述策略。

Trying to understand if there is a strategy for the above.

谢谢!

推荐答案

第一眼看来,你的例子看起来会工作,创建一个内存泄漏。

On first glance, it would seem that your example would work, but in fact it creates a memory leak.

按照惯例在Cocoa和Cocoa-touch中,使用 [[SomeClass alloc] initX] [SomeClass newX] 创建的保留计数为1。当您完成新实例时,通常在 dealloc 方法中,您负责调用 [someClassInstance release]

By convention in Cocoa and Cocoa-touch, any object created using [[SomeClass alloc] initX] or [SomeClass newX] is created with a retain count of one. You are responsible for calling [someClassInstance release] when you're done with your new instance, typically in your dealloc method.

这是棘手的,当你将新对象分配给一个属性而不是一个实例变量。大多数属性定义为 retain copy ,这意味着它们在设置时增加对象的保留计数,

Where this gets tricky is when you assign your new object to a property instead of an instance variable. Most properties are defined as retain or copy, which means they either increment the object's retain count when set, or make a copy of the object, leaving the original untouched.

在您的示例中,您可能在 .h 中具有此对象的副本, file:

In your example, you probably have this in your .h file:

@property (retain) EditingViewController *editingViewController;

因此,在您的第一个示例中:

So in your first example:

EditingViewController *controller = 
    [[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil];
// (1) new object created with retain count of 1

self.editingViewController = controller;
// (2) equivalent to [self setEditingViewController: controller];
// increments retain count to 2

[controller release];
// (3) decrements retain count to 1

但是对于第二个例子: / p>

But for your second example:

// (2) property setter increments retain count to 2
self.editingViewController = 

    // (1) new object created with retain count of 1
    [[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil];

// oops! retain count is now 2

通过调用 autorelease 方法在你的新对象传递给setter之前,你要求autorelease池取得对象的所有权并释放它在未来的一段时间,所以一段时间对象有两个所有者匹配其保留计数,一切都

By calling the autorelease method on your new object before passing it to the setter, you ask the autorelease pool to take ownership of the object and release it some time in the future, so for a while the object has two owners to match its retain count and everything is hunky dory.

// (3) property setter increments retain count to 2
self.editingViewController = 

    // (1) new object created with retain count of 1
    [[[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil]

        // (2) give ownership to autorelease pool 
        autorelease];

// okay, retain count is 2 with 2 owners (self and autorelease pool)


$ b b

另一种选择是将新对象直接分配给实例变量,而不是属性设置器。假设您的代码命名为底层实例变量 editingViewController

// (2) assignment to an instance variable doesn't change retain count
editingViewController = 

    // (1) new object created with retain count of 1
    [[EditingViewController alloc] initWithNibName:@"EditingView" bundle:nil];

// yay! retain count is 1

这是一个微妙但关键的区别。在这些例子中, self.editingViewController = x [self setEditingViewController:x] 的语法糖,但 editingViewController 是一个没有任何由编译器生成的保留或复制代码的简单旧实例变量。

That's a subtle but critical difference in the code. In these examples, self.editingViewController = x is syntactic sugar for [self setEditingViewController: x] but editingViewController is a plain old instance variable without any retain or copy code generated by the compiler.

另请参见为什么会创建内存泄漏(iPhone)?

这篇关于指针/内存管理的可可策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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