Obscure AS3 for-switch-if-false bug (VerifyError)
December 9th, 2008
So we finally tracked down just how Jay broke Grooveshark Lite the other day.
He apparently managed to trigger a very obscure language bug, that no one has really posted about, though if you dig deep enough into Adobe’s bug tracking system, you can find some reports of, though it’s supposedly fixed as of some version of the compiler that I have no idea if it’s in production yet.
This is all it takes to break Actionscript with a nasty runtime error:
private function breakIt():void
{
var arr:Array = [];
for each (var obj:Object in arr) {
switch ('s') {
case 's':
if (null) {
}
//break;
}
}
}
The key here is the nested for/switch/if and that the expression for the if statement evaluates to false, and that there is nothing in the case after the if.
Uncommenting the commented break statement will prevent the bug. Even a variable declaration on that line will prevent the bug. Removing the switch from inside the for loop will prevent the bug. Notice in this case the loop shouldn’t even be executing: the array is empty. Doesn’t matter, it crashes anyway.
The runtime error this triggers is “VerifyError: Error #1068: CLASSNAME and CLASSNAME cannot be reconciled.” where CLASSNAME is the name of the class that contains the above code.
So if you’re getting the above error in your code, check around for any constructs like the above.
<mx:Array> comes in handy for States
December 2nd, 2008
Consider this situation:
You have an MXML component with multiple states. Each of these states has an associated error state that’s based on one of the other states. The overrides for each of the error states is identical – all that’s different is which state each error state is based on. Instead of duplicating the overrides for each error state, you can declare the overrides in an <mx:Array> tag, and use data binding to set the overrides property of each error state to the same array.
Alright, talking about this abstractly is kinda confusing. It will make perfect sense as soon as you see an example:
<mx:Array id="errorOverrides">
<mx:AddChild relativeTo="{msgWrapper}">
<mx:Label text="The following errors occurred:" styleName="popupMessageError"/>
</mx:AddChild>
<mx:AddChild relativeTo="{msgWrapper}">
<mx:VBox paddingLeft="15">
<mx:Repeater id="errorRepeater" dataProvider="{errors}" recycleChildren="true">
<mx:Text width="{msgWrapper.width-45}" text="{errorRepeater.currentItem}"/>
</mx:Repeater>
</mx:VBox>
</mx:AddChild>
</mx:Array>
<states>
<mx:State name="twitterBroadcast">
<mx:SetProperty target="{this}" name="bodyContent" value="{twitterBroadcastBodyContent}"/>
<mx:SetProperty target="{this}" name="title" value="Broadcast to Twitter"/>
</mx:State>
<mx:State name="facebookBroadcast">
<mx:SetProperty target="{this}" name="bodyContent" value="{facebookBroadcastBodyContent}"/>
<mx:SetProperty target="{this}" name="title" value="Broadcast to Facebook"/>
</mx:State>
<mx:State name="shareError" overrides="{errorOverrides}"/>
<mx:State name="twitterError" basedOn="twitterBroadcast" overrides="{errorOverrides}"/>
<mx:State name="facebookError" basedOn="facebookBroadcast" overrides="{errorOverrides}"/>
</states>
This solution seems obvious as soon as you think about how you would go about declaring these states in Actionscript, but in MXML it’s really easy to feel like you would need to declare the overrides over again inside each <mx:State> tag.
Maybe I’m late to the party and everyone else figured this out already, but if not, hopefully this post will help you think about MXML in a new way.
The devil’s in the details…
November 10th, 2008
…says Chris of Grooveshark’s rather disappointing entry for the “Bug of the Beast”:

Repeater performance and recycleChildren
November 7th, 2008
So one of my bug reports this week was that Lite took an insanely long time to populate the list of playlists when you clicked the “Add to Playlist” button on a song’s info panel. We’re talking like, click the button, lightbox opens, 4-5 seconds later your playlists appear. This was happening even for accounts with a small number of playlists.
At first I thought it must be something with the service call to the backend to fetch the list of playlists, but it was returning long before the list rendered.
So then I thought perhaps it was the class I wrote for paging lists of stuff from the backend, but no, parsing the data wasn’t taking that long either.
So I went to make sure I hadn’t done anything silly in the view, like, manually removed and replaced all the children in the box every time the list changed instead of just using a Repeater. But no, I was using a Repeater. Jay says, “What’s this recycleChildren property on the Repeater class?”
I said, “That shouldn’t matter, this is the first time that Repeater’s been rendered. There’s no children created yet *to* recycle. But I guess I’ll set it true anyway.”
And sure enough, now it renders in more like 1 second, as opposed to 4-5. I understand how recycleChildren increases performance when the data in the dataProvider changes. I think it’s silly that it defaults to false – I’d think that most common uses of the Repeater class would benefit from defaulting to true, and if you have a case where you need it false, then you can specify. But I still don’t understand how it’s faster when it’s the first time it’s created any children at all.
So if your Repeaters are slow – set recycleChildren to true!
And if anyone can give some insight into how it’s faster even on the initial creation of the component, let me know. I’m curious.
And for you Grooveshark Lite fans – that change is live, so if you were frustrated by long lag times on the “Add to Playlist” lightbox, the “Share” lightbox, or on opening a playlist or song info panel, it should be much better now.
Comparison of Actionscript 3 vs Java
September 17th, 2008
Just found this today, and thought it was really interesting:
HTML Entities and Actionscript
June 26th, 2008
A couple weeks ago I needed to easily convert HTML entities in a string back to their normal representation, but I didn’t really find anything nice, and wound up just using str.replace(/&/g, "&").replace(/'/g, "'"); since those were the only characters I was having a problem with at the time.
But today I went searching again for something better, and found a really great way to escape and unescape HTML entities. Not sure why I didn’t find his post the first time, I guess my Google-fu was weak that day.
Anyway, I’ve wrapped his methods up in a little helper class, and it works great:
package com.grooveshark.utils
{
public class HTMLEntityUtils
{
import flash.xml.XMLDocument;
import flash.xml.XMLNode;
import flash.xml.XMLNodeType;
public function HTMLEntityUtils()
{
}
public static function htmlEscape(str:String):String
{
return XML(new XMLNode(XMLNodeType.TEXT_NODE, str)).toXMLString();
}
public static function htmlUnescape(str:String):String
{
return new XMLDocument(str).firstChild.nodeValue;
}
}
}
Hope it helps someone else!
Welcome to Grooveshark Lite!
April 15th, 2008
Grooveshark Lite is now officially live.
Grooveshark Lite takes all the long-tail P2P musical goodness of Grooveshark.com and distills it in a handy quick interface to find and listen to whatever you want. Unlike the main Grooveshark site, you don’t have to have an account or install our Sharkbyte client in order to listen to music. However, signing up for an account (it’s super easy!) allows you to save playlists, and your login will work for both Lite and the main Grooveshark site.
We’re hoping that you’ll find Grooveshark Lite to be the easiest way to listen to whatever you can think of.
We have tons of things planned that didn’t quite make it into this iteration, so keep an eye out for exciting new features over the next few weeks. And if you have any comments, suggestions, ideas, wishlists, problems, or just want to say hi, drop us an email at feedback@grooveshark.com. Or you can leave me a comment here and I’ll do my best to answer you personally.
We just upgraded a few other systems at the same time, so if things are a little shaky, just give us a little time and things ought to smooth out.
I’d love to talk more about Grooveshark Lite, but I’ve been here at the office for over 18 hours straight now, trying to make sure my baby gets off the ground right, so I’m not entirely lucid anymore.
Enjoy the music!
Flex and Fonts
April 15th, 2008
This was extremely frustrating, so I’m posting in the hopes that I can save others the same frustration.
So as you may know from my recent posts, I’ve been working on a huge Flex app for Grooveshark. We’re getting ready to launch it (probably in the next few hours actually), and our lead designer John and I were going through the app for some final visual tweaks. Button labels and input boxes were lovingly pushed a pixel here, a pixel there, until they looked just right.
Then we saw the app on Linux. It looked HORRIBLE. Half the text was far too low. You couldn’t even see the tails on letters like ‘g’ and ‘y’. Suspicious, we checked it on a Windows machine. The text was also lower than on my Mac, though not as bad as on Linux. Windows only was only lower by about 4 pixels, Linux more like 10-15. I was baffled. Flash is Flash is Flash, right? The font was an embedded OpenType version of Helvetica that we bought just for this purpose. Why would the font render differently depending on the operating system?
I read about Flash’s different font managers, and thought perhaps the issue was that using an OpenType font forces the use of the AFEFontManager instead of the BatikFontManager. So I tried embedding a TrueType font instead, but it made no difference. The text was still just as skewed in Linux and Windows as it was before.
Eventually I found this blog post and decided that embedding the font via SWF was worth a try. I had originally decided to embed via a font file instead of swf because swf seemed to add more bulk to the compiled app, and honestly, this app is already huge.
Anyway, I embedded Helvetica via SWF instead of OpenType, and it worked! The application now renders identically cross-platform. So if anyone else out there is having trouble with cross-platform font rendering in Flex, try embedding your fonts via SWF.
Nearly there…
April 14th, 2008
Just over 1 Month…
Average of 67.5 hours a week…
Over 21,000 lines of MXML and Actionscript 3…

Me and Jay have been working our asses off for the last month, and all that effort is about to pay off. I’m going to sleep for days…
Installed a theme
March 11th, 2008
Got tired of looking at the default theme, and we’re hitting some serious crunch time at work, so I’m definitely not going to have time to make a personalized theme for at least a month. So, going with a theme for now.