简体   繁体   English

可可中的内存管理

[英]Memory management in Cocoa

I am trying to understand the memory management concepts in Cocoa. 我试图了解Cocoa中的内存管理概念。 I am stuck at the following portion::: 我被困在以下部分::

//This is wrong code. But i want to understand why it is wrong!  
- (void) setEngine: (Engine *) newEngine  
{  
      [engine release];  
      engine = [newEngine retain];  
}  

When newEngine and engine are same, then how does cocoa decides which one to be treated as an instance variable and which one as a parameter? 当newEngine和engine相同时,可可如何决定将哪一个视为实例变量,将哪一个视为参数?

For example: 例如:

Engine *engine = [Engine new]; // count: 1  
Car *car1 = [Car new];  
Car *car2 = [Car new]; 

[car1 setEngine: engine]; // count: 2 -----How is the retain count 2 here? 
[engine release]; // count 1 

[car2 setEngine: [car1 engine]]; 

Please help as i want to learn this concept thoroughly so as to maintain efficiency in my programs. 请帮忙,因为我想彻底学习这个概念,以保持程序的效率。 I am sure a lot many are stuck at this as me and my friends are.. Thanks in advance... :) 我敢肯定,像我和我的朋友们一样,很多人会被困在这里。.在此先感谢... :)

Firstly, you want to make sure your setter is checking to see if the objects are different: 首先,您要确保设置器正在检查对象是否不同:

- (void)setEngine:(Engine *)newEngine {
    if ( engine != newEngine ) {
        [engine release];
        [newEngine retain];
        engine = newEngine;
    }
}

Here's what happens inside this method: 这是此方法内部发生的情况:

  1. Check to see that the engine that has been passed in is different to the one we already have. 检查以确保已传递的引擎与我们已经拥有的引擎不同。 If it is the same, nothing needs to be done. 如果相同,则无需执行任何操作。
  2. Release the old engine. 释放引擎。 If we were the last object to own it, its retain count will be zero and it will be deallocated. 如果我们是拥有它的最后一个对象,则其保留计数将为零,并将被释放。 If the old engine was nil , then the release message will simply be ignored. 如果旧引擎为nil ,则将仅忽略release消息。
  3. Retain the new engine. 保留引擎。 If it is released somewhere else, it will not be deallocated because we have taken ownership of it. 如果它在其他地方发布,将不会被释放,因为我们已经拥有了它的所有权。 If the new engine is nil , then the retain message will simply be ignored. 如果新引擎为nil ,则retain消息将被忽略。
  4. Set the engine instance variable to newEngine . engine实例变量设置为newEngine

Secondly, the reason that the retain count is two after you call setEngine: is because it started as 1 when it was created. 其次,在调用setEngine:之后保留计数为2的原因是因为它在创建时以1开始。 Then you assigned it to car1, which retained it, bringing the retain count to 2. The retain count is two because your outer code owns it and car1 owns it. 然后,将其分配给car1,保留它,使保留计数为2。保留计数为2,因为您的外部代码拥有它, car1拥有它。 You should pass it to car1 and car2 in the same way ( [car2 setEngine:car] ) which would bring the retain count to 3. Once you have passed it along to car1 and car2, you can release it it your outer code - this tells Obcjective-C that your outer code no longer owns it, the ownership lies only with car1 and car2. 您应该以相同的方式将其传递给car1和car2( [car2 setEngine:car] ),这将使保留计数为3。一旦将其传递给car1和car2,就可以将其释放为外部代码-这告诉Obcjective-C您的外部代码不再拥有它,所有权仅属于car1和car2。

Here's a full example: 这是一个完整的示例:

// create an engine, we will own it for now
Engine *engine = [[Engine alloc] init];

/* engine retain count: 1, owned by this context */

Car *car1 = [[Car alloc] init];
Car *car2 = [[Car alloc] init];

// pass engine along to cars
[car1 setEngine: engine];
[car2 setEngine: engine];

/* engine retain count: 3, owned by car1, car2 and this context */

// release engine, we no longer need it
[engine release];

/* engine retain count: 2, owned by car1 and car2 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM