如何在 iPhone / iPad 应用程序中正确使用保留和释放
我是使用 Objective-C 进行 iPhone / iPad 编程的新手。
我在释放或保留对象时遇到内存管理问题。 我正在做一个包含多个用于切换视图的视图控制器的应用程序。
大多数使用的对象都被声明为 IBOutlets,我想知道应该如何以及何时使用保留和释放,以避免出现内存问题并结束观察正确的应用程序。
当切换到另一个视图控制器或者我不必多次使用一个对象时释放它们对我来说会很有趣。
有人可以解释一下我必须如何以及何时使用保留和释放吗?我读过苹果的文档,但我认为它有点令人困惑。我必须将其放在 dealloc 或 didReceiveMemoryWarning 中,但不能解决我的问题。
如果我犯了拼写错误,我很抱歉。 谢谢安丹斯。
I'm a newbie in programming for iPhone / iPad with Objective-C.
I have problems with memory management by releasing or retaing objects.
I'm doing an application that contains multiple view controllers for switching views.
The most of the objects that use are declared as IBOutlets and I would like to know how and when should I use the Retain and release to not have memory problems and ending observing proper application.
It would be interesting for me to release them when switching to another view controller or whem I don't have to use more times an object.
Can someone explain me how and when I must use the Retain and release? I've read Apple's documentation but I think that it's a bit confusing. I've that I must put there in dealloc or didReceiveMemoryWarning, but does not solve my problem.
I'm sorry if I made spelling errors.
Thanks in andance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
如果您将 copy
mutablecopy
retain
或 alloc
发送到一个对象,那么您有责任在以下情况下释放该对象:完了。
NSString *allocedString = [[NSString alloc] initWithString:@"world"];
NSString *myString = [NSString stringWithFormat:@"Hello%@", allocedString];
[allocedString release], allocedString = nil;
您负责释放 allocedString
,但不 myString
(内部使用 autorelease
)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
要查找项目中的泄漏,请使用检测工具运行应用程序,或者通过 shift+command+A 从 xcode 简单运行应用程序。
使用 alloc 和 init 的每个对象都应该被释放,否则会导致内存泄漏。
声明 .h 文件中的每个对象并设置属性并在 dealloc 方法中释放该对象。
仅当您离开页面后必须保留当前状态时才需要保留。
for finding leaks in your project run the application with instrumentation tool or simple run the app from xcode by shift+command+A.
every object that you are used alloc and init should be released else it'll cause memory leak.
declare every object in .h file and set the property and release that obj in dealloc method.
retaining is only require if you have to retain the present state after u left from the page.