XCode 4 +仪器 4:误报泄漏?

发布于 2024-10-25 23:09:50 字数 829 浏览 7 评论 0原文

自从切换到 XCode 4 以来,泄漏工具显示了大量泄漏,全部来自 JSONKit 和 ASIHTTPRequest,运行 2 分钟后,我泄漏了数百个数组/字典/字符串(来自 jk_create_dictionary、jk_parse_array、HTTPMessage::* 等)总计几百KB。大多数堆栈跟踪都不是源自我的任何调用,其余的完全是无辜的。 我非常肯定 XCode 4 之前的情况并非如此。 我不知道罪魁祸首是谁。任何见解都会很可爱。

更新:
JSONKit 泄漏可能是 JSONDecoder 缓存。
例如:

static JSONDecoder *decoder = nil;
if (!decoder) 
    decoder=[[JSONDecoder alloc] init];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setCachePolicy:ASIDoNotWriteToCacheCachePolicy];
[request setCompletionBlock:^{
    NSData *response = [request responseData];
    NSDictionary *json = [decoder objectWithUTF8String:[response bytes] length:[response length]];
    // ...
}];
[request setFailedBlock:^{
    // ...
}];

[request startAsynchronous];

Ever since switching to XCode 4 the leaks tool shows a LOT of leakage, all from JSONKit and ASIHTTPRequest, after a 2 min run I am leaking hundreds of arrays/dictionaries/strings (from jk_create_dictionary, jk_parse_array, HTTPMessage::*, etc.) totaling a few 100s KB. Most of the stack traces don't originate in any of my calls, and the rest are completely innocent.
I am pretty positive it was not the case pre-XCode 4.
I don't know who the culprit is. Any insight would be lovely.

Update:
The JSONKit leaks are probably JSONDecoder caching.
For example:

static JSONDecoder *decoder = nil;
if (!decoder) 
    decoder=[[JSONDecoder alloc] init];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setCachePolicy:ASIDoNotWriteToCacheCachePolicy];
[request setCompletionBlock:^{
    NSData *response = [request responseData];
    NSDictionary *json = [decoder objectWithUTF8String:[response bytes] length:[response length]];
    // ...
}];
[request setFailedBlock:^{
    // ...
}];

[request startAsynchronous];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

还不是爱你 2024-11-01 23:09:50

编辑: 在阅读本答案的其余部分之前:

如果您看到这种内存泄漏,请不要责怪 Instruments 或 JSONKit...两者都很可靠!

...归咎于您自己,99.9% 的可能性您的代码正在泄漏您使用 < 解析的数据代码>JSONKit!

END_OF_EDIT


不是一个答案,更多的是一个补充,并试图了解发生了什么,因为我也看到了仪器的泄漏。

我就是这样使用 JSONKit 的:

NSArray *lines = [dataString componentsSeparatedByString:@"\n"];
for (NSString *line in lines) { // I know, strange format isn't? :)
  NSDictionary *json = [line objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
  // use dictionary data...
}

@ssteinberg,这是你遇到的那种泄漏吗? :
JSONKitLeaks

请注意,我在进行了一些重负载测试后得到了这个,500 个请求和巨大的 JSON 响应,这解释了泄漏是以 MB 为单位使用最新的 gh 版本

请注意,我使用仪器相当新,我不知道如何理解这些结果。据 Frames 报道,是的,这看起来像缓存...但我想确定...

所以我打开了一个 GH 上的问题,我希望@johnezang 或任何人能够告诉我们这一点。

如果这只是对 Instruments 的误解,我深表歉意,我更喜欢这样:)

EDIT : Before you read the rest of this answer:

If you see that kind of memory leaks, don't blame Instruments or JSONKit... Both are reliable!

...Blame yourself, 99.9% chances your code is leaking the data you parsed with JSONKit!

END_OF_EDIT


Not an answer, more a complement, and an attempt to understand what's going on since I'm seeing leaks too with instruments.

I'm using JSONKit that way :

NSArray *lines = [dataString componentsSeparatedByString:@"\n"];
for (NSString *line in lines) { // I know, strange format isn't? :)
  NSDictionary *json = [line objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
  // use dictionary data...
}

@ssteinberg, is that the kind of leaks you're having? :
JSONKitLeaks

Note that I had this after some heavy load testing, 500 requests with huge JSON responses, which explains leaks are in MB (using latest gh version)

Please note that I'm quite new using Instruments, and I'm not sure how to understand these results. According to Frames reported, yes that looks like Caching... but I'd like to be sure...

So I opened an Issue on GH, I hope that @johnezang, or anyone, will enlight us about this.

All my apologies if that's just a misunderstanding with Instruments, which I would prefer :)

花辞树 2024-11-01 23:09:50

根据 Apple 的 WWDC 2010 视频(使用仪器进行高级内存分析),误报泄漏很少见。有时,泄漏工具会漏掉泄漏,但它报告的泄漏是可靠的。我不太擅长静态,但是您是否检查过以确保您没有在未释放解码器的情况下分配解码器?如果它没有发布并且超出范围,那就构成泄漏,对吗?

According to Apple's WWDC 2010 videos (Advanced Memory Analysis with Instruments), false positive leaks are rare. Sometimes the Leaks tool misses leaks, but it's reliable for the ones it reports. I'm not all that great with statics, but have you checked to make sure you're not alloc'ing the decoder without releasing it? If it's not released and falls out of scope, that would constitute a leak, right?

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