iPhone:什么会释放某些东西?释放,直到所有者为0或零? [英] iPhone: What deallocates something? release until owner is 0 or nil?

查看:54
本文介绍了iPhone:什么会释放某些东西?释放,直到所有者为0或零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然对iPhone的内存管理有些困惑.这不是一个大问题,因为它不能处理最常见的用例(我只是在dealloc中释放,而在viewDidUnLoad中设置为nil).但是,让我们直接说吧:

I'm still a tad confused on iPhone memory management. It's not a huge issue as it doesn't deal with the most common use cases (I just release in dealloc, and set to nil in viewDidUnLoad). But let's get this straight:

假设我得到了一个实例变量,该实例变量被保留并合成.我分配它.

Suppose I got an instance variable that is retained and synthesized. And I allocate it.

如果我将其设置为nil,是否意味着该对象已在内存中释放?没了?因为有人告诉我将其设置为nil只会将指针设置为nil,并且不会从内存中取消分配它.

If I set it to nil, does that mean the object is deallocated in memory? It's gone? Because someone told me setting it to nil only sets the pointer to nil, and doesn't deallocate it from memory.

我知道当所有者计数设置为0时,将释放内存.是的,对吗?如果没有,我认为我的整个宇宙可能已经被破坏=)

I know when the owner count is set to 0, the memory is deallocated.. That's correct, right? If not, I think my entire universe may have been broken =)

推荐答案

当一块内存的保留计数为0时,该内存就可以重用(因此将其释放).

When a piece of memory has a retain count of 0 associated with it, then it is available for reuse (so it is deallocated).

假设我得到了一个实例变量,该实例变量被保留并合成.我分配它.

Suppose I got an instance variable that is retained and synthesized. And I allocate it.

在您的.h文件中:

@property (nonatomic, retain) SomeObject myInstanceVariable;

在您的.m文件中:

@synthesize myInstanceVariable;

self.myInstanceVariable = [[SomeObject alloc] init];

此语句有两件事-首先,它为"SomeObject"分配内存,然后在该过程中将该内存的保留计数设置为1.其次,它通过综合的setter方法将指向该内存的指针分配给ivar.合成的setter方法在iVar指向的任何当前内存上调用release,将其保留计数减少1,然后将指向新对象的指针分配给iVar,并增加其保留计数.因此,在此语句之后,保留计数为2

This statement does two things - firstly it allocates memory for "SomeObject" and in the process sets the retain count on that memory to 1. Secondly it assigns a pointer to that memory to the ivar via the synthesized setter method. The synthesized setter method calls release on any current memory pointed to by the iVar, which reduces its retain count by 1, and then assigns the pointer to the new object to the iVar and increments its retain count. Thus after this statement the retain count is 2

如果我将其设置为nil,是否意味着该对象已在内存中释放?没了?因为有人告诉我将其设置为nil只会将指针设置为nil,并且不会将其从内存中取消分配.

If I set it to nil, does that mean the object is deallocated in memory? It's gone? Because someone told me setting it to nil only sets the pointer to nil, and doesn't deallocate it from memory.

self.myInstanceVariable = nil;

这通过综合设置方法设置iVar,该方法将当前值的保留计数减少1,然后在iVar上设置新值(nil).因此,在此示例中,这两个语句一个接一个地导致内存中的保留计数仍为1,因此操作系统无法将其重用.

This sets the iVar via the synthesized setter method, which reduces the retain count of the current value by 1 and then sets the new value (nil) on the iVar. Thus in this example these two statements one after the other result in memory that still has a retain count of 1 and thus is not available for reuse by the operating system.

因此,如果您自己分配一个对象,然后又将其分配给一个keep属性,则应在将其分配给keep属性后显式释放它.(并且在您的dealloc方法中还释放了ivar来满足该属性).

So if you allocate an object yourself and then also assign it to a retain property, then you should explicitly release it after assigning it to the retain property. (and in your dealloc method also release the ivar to cater for the property).

self.myInstanceVariable = [[[SomeObject alloc] init] autorelease];

这会分配一些内存(将其保留计数增加到1),然后通过setter将指向该内存的指针分配给iVar(将保留计数增加到2),但也将其标记为自动释放-这意味着它将被添加到自动释放池,并在线程失去控制时释放(这将使保留计数减少1).然后,当您的对象(自身)被释放时,dealloc方法(如果实现正确)将释放ivar(myInstanceVariable),然后保留计数将变为0,并且最初分配的内存将被释放.

this allocates some memory (increasing its retain count to 1) then assigns a pointer to that memory to the iVar via the setter (increasing the retain count to 2) but also flags it for autorelease - which means it is added to the autorelease pool and released when your thread loses control (which will reduce the retain count by 1). Then when your object (self) is released, the dealloc method (if implemented properly) will release the ivar (myInstanceVariable) and then the retain count will go to 0 and the memory you originally allocated will be deallocated.

这篇关于iPhone:什么会释放某些东西?释放,直到所有者为0或零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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