I am trying to fix a bunch of leaks that my UIWebView
is causing and cannot find their origin nor a workaround. What I do is getting some content from the web through a network request, then assemble my HTML and load it on the fly:
NSString* body = <some HTML>;
NSString* html = [NSString stringWithFormat:kHTMLTemplate, [self scripts], [self styles], body];
[_webView loadHTMLString:html
baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
Each time there is new content available, I execute loadHTMLString
again to refresh the web view. I reuse the same web view, same controller, same everything.
Instruments shows a very strange pattern in which all leaked objects are General-blocks of various sizes and none of them has any information attached to it: no responsible library, no responsible frame, etc. Each time loadHTMLString
is executed, new leaks are added.
It seems that there are several threads in S.O. about UIWebView
leaking memory. I have tried all suggestions I found (e.g., setting the NSURLCache
to zero, or resetting it; I tried releasing the existing UIWebView and allocate a fresh one each time I have new data, etc.) but nothing has helped.
My investigations up to now lead to one clear outcome: it seems that the leaks are only present if the HTML I load into the view contains some Javascript. If you notice the html
string above, it is made up of several components; one is [self scripts]
which is a function that simply returns:
return @"<script type='text/javascript' src='jquery-1.4.4.min.js'></script>"
"<script type='text/javascript' src='jmy.js'></script>";
If I remove this, no leaks are there. But the leaks appear as soon as I add a <script>
tag to my HTML. They even appear if I simply include the jquery file (or any other js file, as to this):
return @"<script type='text/javascript' src='jquery-1.4.4.min.js'></script>";
So, the question: has anyone an idea about what is happening here? Clearly including a Javascript file into my HTML is making the UIWebView
leak memory.
The fact that leaks appear both when I reuse the same UIWebView
object or when I instantiate a new one each time I have content, leads me to think that there must be something in the way javascript files are handled by loadHTMLString
which leads to the leaks.
Does anyone know how this could be fixed?