关于iphone内存管理的问题

发布于 2024-09-25 06:03:54 字数 650 浏览 7 评论 0原文

想象我有一个主视图,其中我调用了“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-10-02 06:03:54

你如何创建 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.

一笔一画续写前缘 2024-10-02 06:03:54

正如 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文