关于iphone内存管理的问题
想象我有一个主视图,其中我调用了“AddSubview”方法,如下所示:
[mainView addsubview:secondView];
...在第二个视图中,我有一个搜索栏,带有适当的代码来显示 UIkeyboard,输入一些文本,使用应用程序中的这一点,最后关闭 UIkeyboard。 好的。
当程序的这一部分完成时,我第二次调用“AddSubview”方法,如下所示:
[secondView removeFromSuperview];
[mainView addsubview:thirdView];
完成后,第二个视图消失,并被第三个视图取代。 但... 第二个视图使用的内存不会被释放。 而且,更重要的是,UIkeyboard 使用的内存(大约 800 Ko)在我退出应用程序之前不会被释放,并且即使当我用第三个视图替换第二个视图时仍然保持活动状态。
那么,有没有办法正确地释放 UIkeyboard 使用的内存?
(精度:在上面的代码中,3个UIviews都被子类化为3个名为ManView、SecondView和ThirdView的类文件,它们分别对应于NIB文件中的3个UIviews对象。在每个文件中我都放置了“dealloc”方法但是,显然,它从未被调用过......)
imagine i have a main view, in which i have a call to the "AddSubview" method, like this :
[mainView addsubview:secondView];
...in this second view, i have a searchBar, with the appropriate code to show a UIkeyboard, type some text, use this one in the app, and finally dismiss the UIkeyboard.
Ok.
When this part of the program is done, i call the "AddSubview" method a second time, like this :
[secondView removeFromSuperview];
[mainView addsubview:thirdView];
When this is done, the second view disappear, and is replaced by the third one.
But...
The memory used by the second view is not deallocated.
And, more important, the memory used by the UIkeyboard (approx. 800 Ko) is not deallocated until i quit the app, and remains active even when i replace the second view by the third view.
So, is there a way to properly deallocate the memory used by the UIkeyboard ?
(precisions : in the above code, the 3 UIviews are all subclassed in 3 class files named ManView, SecondView and ThirdView, which corresponds respectively to 3 UIviews objects in the NIB file. In each of these files i have put the "dealloc" method. But, apparently, it's never been invoked...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你如何创建 secondaryView ..
当您添加子视图时,它会保留,当您删除它时,它会自行释放。
创建 secondaryView 时尝试使用 Autorelease。
或者
从超级视图中删除后释放第二个视图...在释放它之前检查视图是否为零。所以
如果它已被发布,它不会使应用程序崩溃。
How you are creating secondView ..
When you add subView it retains and when you remove it releases it self.
Try using Autorelease when you are creating secondView.
Or
Releasing secondView after removing from superview.... check if view is nil before releasing it. so
if it's been released it wont crash the application.
正如 KiranThorat 指出的那样,addSubview 保留了视图,因此请确保您自己正确地释放它。确保您的视图在 dealloc 方法等中释放任何子视图。
此外,在检查模拟器上的泄漏时,我发现看到一些 UIKeyboard 泄漏是正常的。这些不存在于设备上(至少在我的情况下)。
As KiranThorat points out, addSubview retains the view, so make sure you release it yourself properly. Make sure your views release any subviews in the dealloc method etc.
Also, when checking for leaks on the simulator, I found that it's normal to see some UIKeyboard leaks. These are not present on the device (at least in my case).