简体   繁体   English

在Objective-C中发布问题

[英]Release problems in Objective-C

I would like to ask about the memory management problems in objective C. I am the green of the objective C. When I read some of the sample program from the Apple website, I saw a [XXX release]. 我想问一下目标C中的内存管理问题。我是目标C的绿色代表。当我从Apple网站上阅读一些示例程序时,我看到了[XXX版本]。 I guess this statement is used to release the use of the variable. 我猜想该语句用于释放变量的使用。 However, when I use this statement in my program, I got some problems. 但是,当我在程序中使用此语句时,出现了一些问题。 I used the NSLog() to display the content, but it cannot display the content, it shows some statement about the release. 我使用NSLog()来显示内容,但无法显示内容,它显示了有关发行版的一些声明。

does the objective C has the auto memory management just like java? 目标C是否像Java一样具有自动内存管理功能? or we need to care about the memory problems by the program. 或者我们需要关心程序的内存问题。

Thank you very much. 非常感谢你。

Objective-C does have garbage collection ("auto memory management"), but only on the Mac. Objective-C确实具有垃圾回收(“自动内存管理”),但仅在Mac上才有。 It is not available on the iPhone. 它在iPhone上不可用。 However, the rule of memory management is not that complicated. 但是, 内存管理的规则并不那么复杂。 It is: 它是:

If you were given an object via a method (or function) that contains the word new , alloc , retain , or copy , then you must either release the object or autorelease it. 如果给你通过一个方法(或功能)的对象包含单词newallocretain ,或copy ,那么您必须release对象或autorelease它。

That's pretty much it. 就是这样。 If you always follow this convention, then 99.999% of the time you're going to be OK. 如果您始终遵循此约定,那么您将有99.999%的时间会没事。 The other 0.001% of the time, read the documentation (or ask us here on StackOverflow! :) ). 另外0.001%的时间,请阅读文档(或在StackOverflow上向我们询问!:))。

(I'll also add that anything the documentation says that contradicts this rule wins.) (我还要补充一点,就是文档中说的任何与此规则相抵触的内容都将获胜。)

Read the Memory Management Rules . 阅读内存管理规则 Just knowing those few simple rules will tell you all you need to know about memory management in Objective-C. 只需了解一些简单的规则,就可以告诉您所有有关Objective-C中的内存管理的知识。

You only need to call release on objects you init / alloc yourself or your instance variables in your class dealloc method. 你只需要调用release你的对象init / alloc自己或你的实例变量在类dealloc方法。

Objective-C does have auto release pools. Objective-C确实具有自动发布池。

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

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