Category Archives: Actionscript 2.0

LocalConnection Bug in Flash Player

So I’ve had this problem for ages, and couldn’t find anyone online writing about it.

LocalConnection in AS3 is used to send messages between Flash SWFs in a browser window. It’s also used as a hack to invoke garbage collection in the Flash Player.

I’ve been using it in some recently applications to ensure only one instance of a SWF is loaded at a time on a computer and to display a message to a user if a window with the application is already open.

It’s usually be pretty straight forward.

var LocalConn:LocalConnection = new LocalConnection();

Declares it
then

			try {
				LocalConn.connect("_App");
			} catch(error : ArgumentError) {
trace("Uh oh... Looks like it's already running");
				return;
			}

Only one connection string can be used at a time, and if another SWF in the another browser window (or another tab… or another browser for that matter) tries to connect that ArgumentError will be thrown.

Unfortunately, there is a annoying bug where sometimes, even if no other instances are open, the LocalConnection thinks someone is still running on the same connection string…

I’ve experienced it happening when simply testing in Flash Builder and FDT… Sometimes I am working on another part of my application when one test the application says it’s already running… I’m confused at this point because I know nothing else is running…

I think I’ve narrowed down WHY it happens. It seems to happen when Flash is closed unexpectedly. For example, on Google Chrome, if you run the SWF and then for some reason Flash crashes (you know… the “uh oh Flash crashed” bar that appears at the top of Chrome), it seems that the LocalConnection is never officially closed.

What sucks is that you either have to change the connection string or reboot your computer (restarting browsers don’t work).

If anyone else has experienced this or knows any fixes, please let me know in the comments. I’ve been experiencing this problem since at least Flash Player 9. I’ve reported it on Adobe’s ticketing system… unfortunately they don’t allow anyone to read the ticket because they restricted viewing deeming it a possible “security” flaw – but maybe if other people are experiencing the issue they will put in a fix for it.

Update 5/10/2011
Qingyan Zhu from Adobe replied to my bug report:

Hi Danny,

Thanks for clarifying the issue.

But this is actually as designed.

So when there’s only one browser, if it crashes, then the segment gets reset, and the dangling LC endpoints get cleared.

But if there are two browser processes (depending, say on how IE is launched), or two processes that use LocalConnection (including any AIR app, Flash Projector, AIM, Y!IM, etc etc), then the corrupt memory segment will stick around.

–Qingyan

A Few Things You May Not Know About CacheAsBitmap

cacheAsBitmap is a feature in Flash (since Flash 8) that tells the Flash Player to store a movieclip of vectors into one static instance in memory. It’s a great way to boost the performance of your Flash.

But I’m writing about how to use cacheAsBitmap (you can just google that).

Some things you may never knew about cacheAsBitmap.

  • With the exception of changing position, if a movieclip animates with cacheAsBitmap set to true, performance will slow down. This is because the Flash Player must recalculate the vectors and reload the Bitmap instance into memory. Moving x or y doesn’t change the vectors of the movieclip and thus will not affect performance.
  • cacheAsBitmap is automatically set to true whenever a filter is set on a movieclip.
  • A movieclip moving with cacheAsBitmap set to true, the movieclip will probably not animate smoothly. Unless the displacement values are round numbers, the movieclip will seem to move in a zig zag.

Example:

clip.cacheAsBitmap = true;
clip.onEnterFrame = function() {
this._x += 1;
this._y += 1;
};

Will look fine…

clip.cacheAsBitmap = true;
clip.onEnterFrame = function() {
this._x += 1.5; // Decimal displacement makes things shaky
this._y += 1.5;
};

Won’t.
Just take a look at the following example… Is the red dot shaking or is it just me? Maybe it’s scared of the blue dot.