Mind your semi-colons!

(previously titled : Flash Player bug? for loops)

[UPDATE] Kudos and much thanks to Tim Knip who pointed out that it was in fact my mistake – there’s a stray semi-colon at the end of the line :

for each(byteArray in soundSources); 

Which is of course ending the loop there and just running the code in braces after that once. I seem to keep accidentally typing semi-colons lately. Perhaps it’s the frustration of having to work with the sound API. 🙂

And of course the good news is that we can safely continue to use for each…in. Yay! Thank you everyone for helping me find this.

I’ve been pulling my hair out over the last few days trying to get a sound toy working, don’t you hate it when you feel like you’re fighting against Flash? Anyway, I think I’ve found a bug, I’m iterating through an array of ByteArrays using for each, but for some reason it will only give me the last ByteArray.

I’m sure this has been the source of several unexpected behaviours in my current project. Can you let me know if it happens in your FlashPlayer? I’m currently running the debug player version

Here’s the swf :

[sorry, content no longer available]

And here’s what I see :

ByteArray bug 1

And here’s what I would expect to see :

ByteArray bug 2

You can see I’m iterating through the array twice, firstly with the iterator i and secondly with the for each directive.

And here’s the code :

	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	[SWF(width='200', height='150', backgroundColor='#000000', frameRate='30')]
	public class ByteArrayBug extends Sprite
		public var tf : TextField; 
		public var soundSources : Array; 

		public function ByteArrayBug()
			tf = new TextField(); 
			tf.height = 450; 
			tf.defaultTextFormat = new TextFormat("_sans", 11,0xffffff); 
			soundSources = [makeByteArray(), makeByteArray(), makeByteArray()]; 
			addEventListener(Event.ENTER_FRAME, enterFrame); 
		public function enterFrame(e : Event) : void
			tf.text = ""; 
			var count:int;
			var byteArray : ByteArray;
			for(var i : int = 0; i<soundSources.length; i++)
				byteArray = soundSources[i] as ByteArray; 
				tf.appendText(count+" "+byteArray.length+ " "+soundSources.length+"n"); 
			count = 0;
			for each(byteArray in soundSources); 
				tf.appendText(count+" "+byteArray.length+ " "+soundSources.length+"n"); 
		public function makeByteArray() : ByteArray
			var ba : ByteArray = new ByteArray(); 
			for(var i : int = 0; i< 100 ; i++)
			return ba; 

Let me know what happens and your Flash Player version, and I’ll pass it on to the relevant people. Thanks!

28 replies on “Mind your semi-colons!”

I get the same bug on OSX in
Chrome :: MAC 10,1,51,66 (Debug player)
Safari :: MAC 10,1,51,66 (Debug player)
Firefox :: MAC 10,1,51,66 (Debug player)

I ran into this earlier, but I always figured I might just be doing something wrong…

I have 10,0,32,18 and it didn’t work for me either. I don’t know if this is relevant or not, but a couple of weeks ago I was using a foreach on an array of objects. I got a similar result where the foreach would miss a whole bunch of iteration. When I switched to a for instead it worked fine. I wish I would have saved that code as to me it sounds like we were have same issue.

I’ve just got the ‘bad’ result with 10,0,32,18

Mmm. I’m getting the same if I change the array to an array of objects or arrays (any non-primitive type). Is this a fault (feature) of for-each?

I’ve come across this before, I guess it is another one to chalk up on the list of faults we ignore because we can easily step around it.

yep! and if you wonder why it still displays only 2 results thas because of the textfield height

Another alternative:

soundSources.forEach( echo );
public function echo( ba:ByteArray, index:int, arr:Array ):void
trace( index + ” ” + ba.length + ” ” + soundSources.length );

Well.. no offence but even though your problem was solved, Google indexed the topic – “Flash Player bug? for loops”. Now there are millions of people just looking at it from Google and going away with a wrong idea about flash player in mind.

It’s still a bug although not related to each in. It should not compile. the syntax does not make any sens.

Why doesn’t it?
Most languages will accept it as well.. It’s just an empty statement as the for-loop body.

Yeah MrKishi’s right – it’s not a bug. The for each loop is closed by the semi-colon – it runs through empty code 3 times! A warning in the IDE might be useful though.

It doesn’t because loops come with a code block with curly brackets otherwise there are useless.

not necessarily… consider this :


This is syntactically correct. You only need the curly braces if there is more than one line of code. So :


is the same as running one line of nothing.

It’s the same for if statements and for loops.

Yes thats true. I alway use the brackets even if there is only one line for coherence sake. I must be a maniac.

I have a list on my wall of “stupid things to check first”. This is one of them, along with checking that I have correct semicolons and breaks in switch statements (compiler will not pick up a missing ; and your code won’t work), checking for indexOf(x) == -1 not just indexOf(x) and my favourite, “Have you used addChild?”

Thanks Thomas, I still feel pretty dumb though 🙂 I guess as I was using the Flash Player 10.1 beta I was too quick to assume I’d found a problem with it. A big lesson for us! 🙂

Comments are closed.