gpt4 book ai didi

iphone - 固有的 UIView 泄漏

转载 作者:行者123 更新时间:2023-12-03 21:24:42 26 4
gpt4 key购买 nike

当在事件监视器中运行时,运行以下代码的程序的实际内存使用量将无限增加:

CGRect frame = CGRectMake(0,0,0,0);
while(true)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
UIView *test = [[UIView alloc] initWithFrame:frame];
[test release];
[pool release];
}

发生的情况是,所有从 UIView 派生的对象都会泄漏。有些会比其他泄漏更多(UITextView 特别引起了人们对这个问题的关注)。这些泄漏实际上并未在泄漏监视器中发现 - 它们的存在只能通过内存使用量的不断增加来揭示 - 这最终导致应用程序因内存耗尽而被操作系统终止。

以前有人注意到这一点吗?郑重声明,该代码是针对 OS 3.0 编译的。

最佳答案

我猜这是仪器的问题。使用 iPhone OS 3.0 时,仪器无法正常工作,例如你看不到堆栈跟踪。在模拟器中使用 3.1 时,此问题消失(参见图片)。事实上,这些在 Instruments 中并未显示为泄漏,这一事实有助于我的假设。

当然也有可能这确实是 iPhone OS 3.0 的问题,并且已在 iPhone OS 3.1 中修复。

Instruments with OS 3.0
(来源:hillrippers.ch)

^^ 配备OS 3.0的仪器

Instruments with OS 3.1
(来源:hillrippers.ch)

^^ 配备OS 3.1的仪器

这是使用的代码(在 applicationDidFinishLaunching: 中)

NSUInteger i = 0;
CGRect frame = CGRectMake(0.f, 0.f, 100.f, 50.f);
while (i < 100000) {
UIView *test = [[UIView alloc] initWithFrame:frame];
[test release];
i++;
}

关于iphone - 固有的 UIView 泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1378194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com