简体   繁体   English

可可触摸-内存管理

[英]Cocoa touch - Memory Management

I am a former java programmer, and I am having some troubles managing the memory on cocoa touch. 我是一名前Java程序员,在管理可可触摸内存时遇到一些麻烦。 In fact, I think I got the retain/release trick, but still I am not sure I got it right. 实际上,我认为我有保留/释放技巧,但仍然不确定我是否正确。

For example, I am creating and adding a subview to the main window: 例如,我正在创建一个子视图并将其添加到主窗口:

aViewController=[[AViewController alloc]init];//aViewController is (nonatimic,assign), so retaincount = 1 after this line?
[self.window addsubview aViewController];
[aViewController release];//retaincount=0?

And in aViewController I have an IBAction: 在aViewController中,我有一个IBAction:

[self.view removeFromSuperView];

How can I be sure the object aViewController gets completely 'deleted' and memory released after I removed it from superview (think that controller as a graphic-heavy view controller)? 我如何确定将aViewController对象从超级视图中删除后将其完全“删除”并释放内存(将其视为图形密集型视图控制器)?

Also, generally, is there a way to be sure an object is deallocated? 而且,通常,是否有办法确保对象被释放? I am aware that if I ask ownership of an object I have to release it at a certain point, but what if I just want the object's pointer to be null at a certain point(not basing on the retaincount)? 我知道,如果我要求某个对象的所有权,我必须在某个点释放它,但是如果我只希望该对象的指针在某个点为空(而不是基于keepcount),该怎么办? Should I call dealloc directly? 我应该直接调用dealloc吗? I find sometimes very confusing to keep under control the retain/release mechanism. 我发现有时很难控制保留/释放机制。

If someone could give me a quick breakdown to make my mind 'click', i would be extremely grateful. 如果有人可以快速细分我的想法,让我“点击”,我将非常感激。 Thanks. 谢谢。

The short answer is you shouldn't worry about when an object gets deallocated (unless you are debugging a memory management problem). 简短的答案是,您不必担心何时释放对象(除非您正在调试内存管理问题)。 You should just worry about ensuring that if your code retains, copies or inits an object, it releases or autoreleases it. 您应该担心要确保如果代码保留,复制或初始化了对象,则它会释放或自动释放它。 By doing so you will ensure reference counts are properly maintained and hence deallocation will be managed for you. 这样,您将确保正确维护引用计数,因此将为您管理释放。

Leave the task of deciding when to dealloc an object to the runtime. 保留决定何时将对象取消分配给运行时的任务。 Never call dealloc directly unless you are calling the super classes dealloc method at the end of your objects dealloc method. 除非直接在对象dealloc方法的末尾调用超类dealloc方法,否则切勿直接调用dealloc。

Also, don't even look at the retain count property of an object. 另外,甚至不要看对象的保留计数属性。 Various pieces of the framework manipulate those too during the lifetime of the object, and you'll see that number move around seemingly at random. 在对象的生存期内,框架的各个部分也对它们进行操作,并且您会看到该数字似乎是随机移动的。 It'll just drive you nuts. 它只会使您发疯。

The really important thing is to make sure you've got the objects retained that would be a problem if they went away suddenly, and released when you're okay with them going away suddenly. 真正重要的是要确保您保留了保留的对象,如果它们突然消失会很成问题,并且在您认为它们会突然消失时可以释放它们。

aViewController=[[AViewController alloc]init];
retainCount is 1 keepCount为1
[aViewController removeFromSuperView];
retainCount is 2 (adding the view increments the retainCount) keepCount为2(添加视图会增加keepCount)
 [aViewController release]; [aViewController版本]; 
retain count decrements to 1; 保留计数减为1;
 [aViewController removeFromSuperView]; [aViewController removeFromSuperView]; 
retain count decrements to 0; 保留计数减为0;
Now the dealloc method will be called the allocated memory will be freed. 现在将调用dealloc方法,将释放已分配的内存。 This is what have understood please correct me if i am wrong i always find difficulties during memory management. 这就是我所理解的,如果我错了,请纠正我,我在内存管理过程中总是会遇到困难。

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

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