释放或不释放控制器
首先,对我的英语(我是西班牙语)和新手感到抱歉。我正在开发第二个用于学习的 iPad 应用程序,但我遇到了内存管理问题。我现在面临这个问题大约一个月了,所以我想也许这个伟大社区的一些专家可以帮助我一点。
我的场景是:
这是一个简单的电子书应用程序。我有一个主视图,用户可以从中打开两个模式视图。主要问题在于模态视图之一。在那里我有一个管理所有页面的滚动视图。现在有12页。我试图释放所有内容等,但有些东西仍然保留在内存中,因为在仪器中内存不断增长,当我关闭模态视图并在几次后回来时,它崩溃了。这些页面是单独的 xib,我用这种方法加载它们:
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= MyNumberOfPages) return;
if ((NSNull *)controller != [NSNull null]) {
NSString *className = [NSString stringWithFormat:@"Pagina%d", page];
Class myClass = NSClassFromString(className);
controller = [[myClass alloc] initWithNibName:className bundle:nil];
// I have tried autorelease and even retain] autorelease.
[viewControllers replaceObjectAtIndex:page withObject:controller];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
if (page != 0){
controller.view.tag = page;
}else {
controller.view.tag = 9999;
}
[scrollView addSubview:controller.view];
[scrollView sendSubviewToBack:controller.view];
[controller release];
}
}
我每次加载 3 个页面,可见的页面和左侧和右侧的页面。我尝试以这种方式卸载(删除或摆脱)它们(对于加载的 3 个周围的):
[viewControllers removeObjectAtIndex:pagi - 2];
[[scrollView viewWithTag:pagi - 2] removeFromSuperview];
所以我有两个问题...如果我像上面的方法中所做的那样释放控制器,我的 IBActions 在一些页面使应用程序崩溃。但如果我无论如何释放它并删除 IBActions,内存也会不断增长。在 Instruments 中,它没有显示泄漏,但在增加 1mb 或 2mb 内存后崩溃。
我已经记录了所有页面 viewDidUnload 和 deallocs,它们仅在出现内存警告时才卸载。
最后,对我来说比较奇怪的是,如果我不使用[controllerrelease];应用程序停留时间更长而不会崩溃。但当然,他们不会解除分配。
我认为问题可能出在这个方法中,但如果有帮助,我可以上传更多部分的代码。只是为了不要在这里待太久。
有什么想法吗?
First of all, sorry for my english (I'm spanish) and for being a newbie. I am developing my second iPad app for learnign and I am having problems with the memory management. I am facing this problem for about a month now, so I have thought that maybe some expert in this great community could help me a bit.
My scenario is:
It is a simple e-book app. I have a main view from which the user can open two modal views. The main issue is in one of the modal views. In there I have a scrollview that manages all the pages. Right now, there are 12 pages. I have tried to release everything, etc, but something remains in memory, because in instruments memory keeps growing and when I dissmiss the modal view and come back, after a few times, it crashes. The pages are separate xibs and I load them with this method:
- (void)loadScrollViewWithPage:(int)page {
if (page < 0) return;
if (page >= MyNumberOfPages) return;
if ((NSNull *)controller != [NSNull null]) {
NSString *className = [NSString stringWithFormat:@"Pagina%d", page];
Class myClass = NSClassFromString(className);
controller = [[myClass alloc] initWithNibName:className bundle:nil];
// I have tried autorelease and even retain] autorelease.
[viewControllers replaceObjectAtIndex:page withObject:controller];
CGRect frame = scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
if (page != 0){
controller.view.tag = page;
}else {
controller.view.tag = 9999;
}
[scrollView addSubview:controller.view];
[scrollView sendSubviewToBack:controller.view];
[controller release];
}
}
I load 3 pages each time, the one visible and left and right ones. I try to unload (remove or get rid of) them this way (for the ones around the 3 loaded):
[viewControllers removeObjectAtIndex:pagi - 2];
[[scrollView viewWithTag:pagi - 2] removeFromSuperview];
So I have two problems... If I release the controller as I am doing in the method above, my IBActions in some pages crash the app. But If I release it anyway, and remove IBActions, the memory keeps growing too. In Instruments, it shows no leaks, but it crashes after growing 1mb or 2mb of memory.
I have logued all the pages viewDidUnload and deallocs and they unload only when there is a memory warning.
Finally, what is more strange for me, is that if I don't use [controller release]; the apps stays more time without crashing. But of course, they don't dealloc.
I think that the problem may be in this method, but if it helps, I could upload more parts of the code. It's just to don't make it too long here.
Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嘿豪尔赫。
首先我得说你的英语还不错。我来自瑞士,如果你没有说你是西班牙人,我就不会注意到。 ;-)
一般来说,我可以说你必须在应用程序的此时释放控制器。上面几行,你调用了
所以你必须释放它,因为你分配了它。如果您使用 alloc、retain 或 copy,则您有责任释放这些对象。
但我猜你的问题是什么。你是如何定义
控制器
的?它是 iVar 还是财产?如果它是一个属性,那么内存管理就会出现问题,因为“旧”值没有被释放。在这种情况下,请使用self.controller
而不是controller
。否则我无法想象为什么应用程序在只有此代码可用的情况下崩溃。 ;-)
桑德罗·迈尔
Hey Jorge.
First I have to say that your english is not bad. I'm from switzerland and if you hadn't said you are spanish, I wouldn't have noticed it. ;-)
In generall, I can say you have to release the controller at this point of the applicaiton. A few lines above, you call
So you have to release it, because you allocated it. If you use alloc, retain or copy you are responsible of releasing these objects.
But I have a guess what your problem is. How did you define
controller
? Is it an iVar or also a property? If It's a property, you have a problem with the memory management, because the "old" value is not released. In this case useself.controller
instead ofcontroller
.Else I can't imagine why the application crashes with only this code available. ;-)
Sandro Meier