Saturday, February 6, 2010

Why does Safari/Flash always crash?

On a personal note, I have started a new job at Kontera, involving much more web-related development than I have ever done. I will continue posting experiences from this job I find interesting and worthy of sharing.

Safari users may have noticed often crashes when visiting sites that contain Flash. I certainly have. This is not related to a specific OS - it happens both on Windows and on Mac (I personally use a Mac). I noticed this when developing a web component that includes also some Flash, and easily reproduced quite frequent crashes on Safari 4.0.4 and Flash 2010.

Interestingly enough, Chrome which is based on the same engine does not have these crashes.

From examining the stack of Safari (and also knowing the behavior of my flash app ;-), I think that the crash happens when Safari is trying to do something while Flash is calling JavaScript code via Reducing the number of calls from Flash to JavaScript and reducing the time spent in the JavaScript calls causes the probability of a crash to be reduced dramatically. The simplest technique is to modify the JavaScript callback to wrap it in a setTimeout(...) as such:

function foo(param1, param2) { .....}

should be replaced with:

function foo(param1, param2, afterTimeout) {
if (!afterTimeout) {
setTimeout(function() { foo(param1, param2, true) }, 20);
.... // the original code

The flash code does not need to be modified at all.

Happy hacking,


1 comment:

robert d said...

And you are content to accept this as a fix. We have 2 of the biggest players in the game - Apple and Adobe - and neither one is responding to an issue of significant magnitude.

Actually the ball is in Apple's court and their total ignore of the Flash problem is another example of the bean counters taking everything over.

Some of the more gifted people I know are unplugging.

Life is good when things work,