在UIWebView中调用loadRequest时,GeneralBlock-56内存泄漏 [英] GeneralBlock-56 memory leak when calling loadRequest in UIWebView

查看:107
本文介绍了在UIWebView中调用loadRequest时,GeneralBlock-56内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个IPad应用程序并使用XCode 4中的Instruments进行分析。
我正在分析实际的设备本身。安装了iOS 4.3。



乐器告诉我有一些内存泄漏,泄漏的对象主要是 GeneralBlock-56 一些和一些 GeneralBlock-1024 / GeneralBlock-8192
有趣的是,只有当我在我的应用程序中嵌入的UIWebView中加载URL时,才会报告这些泄漏。如果我注释掉 loadRequest 调用,这些泄漏就会消失。此行为始终可重现。



loadRequest()调用如下所示:

  [webPage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@http://slashdot.org]]]; 

顺便说一下,我没有为这个UIWebView指定任何代理。
泄漏没有显示任何负责任的图书馆/负责任框架,最右边的框架中没有扩展详细信息。



我试图摆弄NSURLCache设置,如下所示:

  NSURLCache * sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]; 
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

这减少了一些GeneralBlock内存泄漏,但仍有一些仍然存在。
非常欢迎任何有关此行为的见解......谢谢!

解决方案

这是我提交的错误报告Apple Developer Bug报告工具。这似乎是导致内存泄漏的重大问题之一。



摘要:将格式错误的NSURL传递给NSData dataWithContentsOfURL:导致createCanonicalURL中的内存泄漏()方法。



重现步骤



任何简单的项目,在控制器中放置以下行,例如,在loadView:或viewDidLoad:methods。:

  NSURL * u = [NSURL URLWithString:@http:/ portalqa01:70 / Images / Leading%20out%20of%20a%20Downturn%20-%20Article%20Illustration%20-%20Large_tcm137-38905.gif]; 
NSData * data = [NSData dataWithContentsOfURL:u options:0 error:nil];

请注意,URL在http:之后只有一个斜杠,而不是惯用的两个斜杠。 / p>

预期结果:不应有任何内存泄漏。



实际结果:仪器显示内存泄漏。



回归:
这仅适用于某些格式错误的网址,上面的网址是一个具体示例。像http:/blah/blah.png这样的网址不会导致泄密。
用于重现这个的环境是:




  • iPad 4.3模拟器

  • iPad 2 iOS 4.3.1



注意:
堆栈跟踪如下:



STACK TRACE START



  0 CFNetwork createCanonicalURL 
1 CFNetwork HTTPProtocol :: _ createMutableCanonicalRequest(__ CFAllocator const * ,_CFURLRequest const *,void const *)
2 CFNetwork HTTPProtocol :: _ createCanonicalRequest(__ CFAllocator const *,_ CFURLRequest const *,void const *)
3 CFNetwork HTTPProtocol :: copyCanonicalRequest()
4 CFNetwork URLConnectionLoader :: copyProtocolCanonicalRequest()
5 CFNetwork URLConnectionClient :: getRequestForTransmission(unsigned char,_CFURLResponse *,_ CFURLRequest const *,__ CFRror **)
6 CFNetwork URLConnectionClient :: _ clientWillSendRequest(_CFURLRequest const *,_ CFURLResponse *,URLConnectionClient :: ClientConnectionEventQueue *)
7 CFNetwork URLConnectionClient :: C. lientConnectionEventQueue :: processAllEventsAndConsumePayload(XConnectionEventInfo< XClientEvent,XClientEventParams> *,长)
8分配CFNetwork的URLConnectionClient :: processEvents()
9分配CFNetwork的MultiplexerSource ::执行()
10分配的CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
11 CoreFoundation __CFRunLoopDoSources0
12 CoreFoundation __CFRunLoopRun
13 CoreFoundation CFRunLoopRunSpecific
14 CoreFoundation CFRunLoopRunInMode
15 CFNetwork CFURLConnectionSendSynchronousRequest
16 Foundation + [NSURLConnection sendSynchronousRequest:returningResponse:error:]
17基础 - [NSData(NSData)initWithContentsOfURL:options:error:]
18 Foundation + [NSData(NSData)dataWithContentsOfURL:options:error:]
19 MemLeakTester - [MemLeakTesterViewController viewDidLoad] / Users / admin /IPadSpikes/MemLeakTester/MemLeakTester/MemLeakTester/MemLeakTesterViewController.m:36
20 UIKit - [UIViewController视图]
21 UIKit - [UIWindow addRootViewControllerViewIfPossible]
22 MemLeakTester - [MemLeakTesterAppDelegate应用程序:didFinishLaunchingWithOptions:] /Users/admin/IPadSpikes/MemLeakTester/MemLeakTester/MemLeakTester/MemLeakTesterAppDelegate.m:27
23 UIKit - [UIApplication _callInitializationDelegatesForURL:payload:suspended:]
24 UIKit - [UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:]
25 UIKit - [UIApplication handleEvent:withNewEvent:]
26 UIKit - [UIApplication sendEvent:]
27 UIKit _UIApplicationHandleEvent
28 GraphicsServices PurpleEventCallback
29 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
30 CoreFoundation __CFRunLoopDoSource1
31 CoreFoundation __CFRunLoopRun
32 CoreFoundation CFRunLoopRunSpecific
33 CoreFoundation CFRunLoopRunInMode
34 UIKit - [UIApplication _run]
35 UIKit UIApplicationMain
36 MemLeakTester main /Users/admin/IPadSpikes/MemLeakTester/MemLeakTester/MemLeakTester/main.m:14
37 MemLeakTester start






STACK TRACE END


I'm working on an IPad app and have been profiling it using Instruments in XCode 4. I'm profiling on the actual device itself. iOS 4.3 is installed.

Instruments tells me I have some memory leaks, the leaked objects being mainly GeneralBlock-56 ones and some GeneralBlock-1024/GeneralBlock-8192 ones. The interesting thing is that these leaks are reported only when I load up a URL in a UIWebView embedded in my app. If I comment out the loadRequest call, these leaks go away. This behavior is consistently reproducible.

The loadRequest() calls looks like this:

[webPage loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://slashdot.org"]]];

I'm not specifying any delegates for this UIWebView, by the way. The leaks do not show any Responsible Library/Responsible Frame and there is no Extended Detail in the rightmost frame.

I have tried to fiddle around with the NSURLCache settings, like so:

NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];

This reduces a few of the GeneralBlock memory leaks, but some still remain. Any insights on this behavior are very welcome...thanks!

解决方案

This is a bug report I filed with the Apple Developer Bug Reporting tool. This seemed to be one of the big issues causing the memory leak in my case.

Summary: Passing incorrectly formatted NSURL to NSData dataWithContentsOfURL: causes memory leak in createCanonicalURL() method.

Steps to Reproduce

In any simple project, put the following lines in your controller, for example, in loadView: or viewDidLoad: methods.:

NSURL* u = [NSURL URLWithString:@"http:/portalqa01:70/Images/Leading%20out%20of%20a%20Downturn%20-%20Article%20Illustration%20-%20Large_tcm137-38905.gif"];
    NSData* data = [NSData dataWithContentsOfURL:u options:0 error:nil];

Note that the URL has only a single slash after "http:" instead of the customary two.

Expected Results: There shouldn't be any memory leak.

Actual Results: Instruments shows a memory leak.

Regression: This only occurs for some malformed URLs, the URL above is a specific example. A URL like "http:/blah/blah.png" will not cause a leak. The environments used to reproduce this were:

  • iPad 4.3 Simulator
  • iPad 2 with iOS 4.3.1

Notes: The stack trace is as below:

STACK TRACE START

   0 CFNetwork createCanonicalURL
   1 CFNetwork HTTPProtocol::_createMutableCanonicalRequest(__CFAllocator const*, _CFURLRequest const*, void const*)
   2 CFNetwork HTTPProtocol::_createCanonicalRequest(__CFAllocator const*, _CFURLRequest const*, void const*)
   3 CFNetwork HTTPProtocol::copyCanonicalRequest()
   4 CFNetwork URLConnectionLoader::copyProtocolCanonicalRequest()
   5 CFNetwork URLConnectionClient::getRequestForTransmission(unsigned char, _CFURLResponse*, _CFURLRequest const*, __CFError**)
   6 CFNetwork URLConnectionClient::_clientWillSendRequest(_CFURLRequest const*, _CFURLResponse*, URLConnectionClient::ClientConnectionEventQueue*)
   7 CFNetwork URLConnectionClient::ClientConnectionEventQueue::processAllEventsAndConsumePayload(XConnectionEventInfo<XClientEvent, XClientEventParams>*, long)
   8 CFNetwork URLConnectionClient::processEvents()
   9 CFNetwork MultiplexerSource::perform()
  10 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
  11 CoreFoundation __CFRunLoopDoSources0
  12 CoreFoundation __CFRunLoopRun
  13 CoreFoundation CFRunLoopRunSpecific
  14 CoreFoundation CFRunLoopRunInMode
  15 CFNetwork CFURLConnectionSendSynchronousRequest
  16 Foundation +[NSURLConnection sendSynchronousRequest:returningResponse:error:]
  17 Foundation -[NSData(NSData) initWithContentsOfURL:options:error:]
  18 Foundation +[NSData(NSData) dataWithContentsOfURL:options:error:]
  19 MemLeakTester -[MemLeakTesterViewController viewDidLoad] /Users/admin/IPadSpikes/MemLeakTester/MemLeakTester/MemLeakTester/MemLeakTesterViewController.m:36
  20 UIKit -[UIViewController view]
  21 UIKit -[UIWindow addRootViewControllerViewIfPossible]
  22 MemLeakTester -[MemLeakTesterAppDelegate application:didFinishLaunchingWithOptions:] /Users/admin/IPadSpikes/MemLeakTester/MemLeakTester/MemLeakTester/MemLeakTesterAppDelegate.m:27
  23 UIKit -[UIApplication _callInitializationDelegatesForURL:payload:suspended:]
  24 UIKit -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:]
  25 UIKit -[UIApplication handleEvent:withNewEvent:]
  26 UIKit -[UIApplication sendEvent:]
  27 UIKit _UIApplicationHandleEvent
  28 GraphicsServices PurpleEventCallback
  29 CoreFoundation __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__
  30 CoreFoundation __CFRunLoopDoSource1
  31 CoreFoundation __CFRunLoopRun
  32 CoreFoundation CFRunLoopRunSpecific
  33 CoreFoundation CFRunLoopRunInMode
  34 UIKit -[UIApplication _run]
  35 UIKit UIApplicationMain
  36 MemLeakTester main /Users/admin/IPadSpikes/MemLeakTester/MemLeakTester/MemLeakTester/main.m:14
  37 MemLeakTester start


STACK TRACE END

这篇关于在UIWebView中调用loadRequest时,GeneralBlock-56内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆