iOS: The error "message sent to deallocated instance" occurred due to the release of CFRunLoopTimer. Let

I'm currently developing an iOS application for iOS 7, where I am running JavaScript on UiWebView multiple times. However, I often encounter a crash with the error message: '[CFRunLoopTimer release]: message sent to deallocated instance'. Occasionally, I also face errors such as 'webthread multiple thread locks not allowed'.

Despite confirming that only one thread is interacting with the UIWebView at a time, I have experimented with executing operations on various threads and queues (background, main, and global) with no success in resolving the issue.

While using Instruments in zombie mode to profile the application and reproduce the crash, I receive further insights.

Although I have meticulously checked my JavaScript code and confirmed its correctness, the error persists frequently. I have even set NSZombieEnabled = YES but to no avail.

If anyone can provide a solution or debugging tip to help resolve this issue, it would be greatly appreciated.

Answer №1

I have been dealing with a persistent bug that has plagued me for quite some time.

Despite trying various methods such as NSOperationQueue's addOperationWithBlock:, dispatch_async, and

performSelectorOnMainThread:withObject:waitUntilDone:
, I was unable to find a solution. It felt like I was grasping at straws.

Eventually, I discovered that my issue lay in how I was calling my JavaScript code using

[myJsContext evaluateScript:jsString];
, obtaining the JSContext from the UIWebView through
[myWebView valueForKeyPath: @"documentView.webView.mainFrame.javaScriptContext"];
.

Instead of this, I decided to revert back to using

[myWebView stringByEvaluatingJavaScriptFromString:jsString];
, and surprisingly, it resolved my problem (at least for now).

Perhaps this could be a potential solution for you as well if you are working with UIWebViews? My understanding is that evaluateScript: is more suitable when utilizing a JSContext without a UIWebView.

(While my answer may lack technical precision, it has proven effective for me! Feel free to explore further if you are well-versed in JavaScriptCore.)

Answer №2

After investigating, I have found a solution to this crash. Simply perform a dummy stringByEvaluatingJavaScriptFromString on the UIWebView before calling a method on the context. This issue seems to be related to the timing of the call to JavaScript on the Web Thread and how it receives replies back to the main thread. Without the creation of a timer, invoking the method results in a crash when attempting to release an uncreated timer. By utilizing the proper API stringByEvaluatingJavaScriptFromString, the necessary timer is established for use by invokeMethod.

JSContext* context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
JSValue* value = context[@"Colors"];

// Fix for timer CFRelease crash
[webView stringByEvaluatingJavaScriptFromString:nil]; 

[value invokeMethod:@"update" withArguments:@[objectID, modifier]];

Answer №3

To resolve the issue, I recommend conducting a thorough search for all instances of NSTimer within your project. Check to see if any object is unintentionally releasing it multiple times. By setting breakpoints on each method where the timer is released, you can pinpoint when this occurs.

Another helpful tip is to utilize a profiler to track all object allocations and enable NSZombie for identifying memory management problems.

Answer №4

It is crucial to remember that only the main thread should be used when calling methods on UIWebView. UIWebView is not designed to handle multiple threads, so making calls from various threads can result in unsafe behavior.

Although you may believe that only one thread is interacting with UIWebView at a given time, unfortunately, this is not sufficient. Other factors such as runloop timers and user interactions also play a role in accessing the web view. Therefore, caution must be exercised when working with UIWebView.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporate Subtitles into Your Website Using JWPlayer

I want to incorporate Video Captions similar to those seen on Lynda.com, for example at The captions should synchronize with the player and also appear in a separate block of HTML below the player. I am using JWPlayer for my video and have successfully in ...

Tips for extracting the image URL from my JSON string

I am in possession of a json file that includes URLs for images, and I have come across something that seems to be a URL, which is encoded as: iVBORw0KGgoAAAANSUhEUgAAADYAAAAzCAMAAADrVgtcAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6m ...

Executing API calls directly within the `useEffect()` function

I am inquiring about the functionality of useEffect() in a function component. This script fetches data from a server and displays it. const NameList = (props) => { const [result, setResult] = useState([]); useEffect(() => { var url = `http ...

onsubmit function was never triggered

Seems like a silly mistake, but I'm encountering an issue with my HTML form. Here's a snippet of the code: <form onsubmit="updateProfile();"> <input type="submit" value="Update Account"> .. ... </form> However, w ...

"Sparkling div animation with the use of jQuery's .hover() function

<script> $(document).ready(function () { $("#logo_").hide(); $("#logo_learn").hide(); }) function sl() { $("#logo_learn").slideToggle(500); $("#logo_").fadeIn(); } function hl() { $("#logo_learn").slideToggle(500); $("#logo_ ...

A step-by-step guide to setting up a Slick Slider JS slideshow with center mode

I am working on implementing a carousel using the amazing Slick Slider, which I have successfully used for images in the past without any issues. My goal is to create a 'center mode' slideshow similar to the example provided but with multiple div ...

Encountering issues due to overriding JSON.stringify

Recently, I developed a customized function for JSON.stringify implementation as shown below: JSON.stringify = function (item, replacer, space) { return JSON.stringify(item, replacer, space); } This modification led to multiple errors in AngularJS w ...

PHP AJAX Serial Port Text Input Command

Recently, I've been delving into the world of PHP and AJAX. Despite being a newcomer, I've managed to grasp some basic concepts over the past few weeks. Along the way, I've relied on various posts from this platform for guidance, so when a p ...

Unable to connect to React Node Socket.IO application from any source other than localhost using IPv4 and NGROK

I recently followed a tutorial on creating a simple React app/Node with Socket.IO. The tutorial worked perfectly, so I decided to test it with two other devices: One PC on the same wifi network (via IPv4 - 192.168.1.8:3000) A mobile device using Ngrok tun ...

Ensuring data is saved on multiple forms using ui-router in AngularJS

Implementation Details: I'm currently utilizing ui-router to load a form page in a ui-view div. I found an excellent example by Andres Ekdahi. My question is, how can I perform a $dirty check on multiple forms using the same directive? Form 1 < ...

Failed to load TypeScript file or similar issue

Whenever I attempt to generate a TypeScript file from a partial view (MVC .NET) that is loaded through a rest call and then appended to a div element, I encounter an error in my console. The error message reads: Uncaught ReferenceError: xyz is not defined ...

The color of the progress bar in JS is not displaying properly

My work involves using jQuery to manipulate progress bars. The issue I am facing is defining standard colors that should be displayed on the progress bar based on the value received. Here is my code: var defaultSegmentColors = ['#FF6363', &ap ...

Manage and update events in the Angular Bootstrap Calendar

For the past few days, I have been working on integrating a calendar into my project. I decided to repurpose an example provided by the developer. One issue that I've come across is related to the functionality of deleting events when using the dropdo ...

What is the best way to replace a state without causing any mutations?

Currently, I am utilizing React and Redux and facing the task of updating my state with new data. Within my state lies an array of objects structured like this: [ { "messages": [/*somedata*/], "users": [/*somedata*/] ...

JS Finding all individuals sharing a common last name

I recently started coding and came across this problem that I'm struggling to solve. The task at hand is to create a function that takes an array of full names and returns only those with the last name "Smith". Example: ['Charlotte Jones' ...

jQuery Mobile is experiencing page height inaccuracies

My web application, built with jQuery Mobile 1.2.0, is encountering an issue with page height calculations on Windows Phone. While iOS and Android display correctly, there is a gap at the bottom of the page on Windows Phone. Is there a CSS-only solution t ...

Has the Field Validity Been Verified and Are all Fields Completed Prior to Submitting?

I have made changes to this question and am seeking clarification. In my project, I have a form with multiple fields, some of which require regex validation. Currently, I have a function that checks if all fields are filled before enabling the submit butto ...

Making adjustments to regular expressions

In my asp.net application, I have a text box where users input a URL and I am using a regular expression for validation. The current regular expression looks like this: ^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(&bsol ...

Automatically updating div content using a static HTML page

Is there a way to refresh the content of an HTML div tag every 5 minutes without having to reload the entire page? <div id="pie"> <script src="index.js">// this script should be reloaded every 5 minutes </script& ...

Guide to loading Unsplash images in Gatsby with the gatsby-source-unsplash plugin

Struggling to use Gatsby to load images from Unsplash by utilizing both gatsby-source-unsplash and gatsby-plugin-remote-images. Below is my configuration: gatsby.config.js: { resolve: `gatsby-source-unsplash`, options: { appId: `${pr ...