Thursday, July 24, 2008
MAX!
Registration Now Open! Register now for MAX North America. Learn more at http://www.max.adobe.com/
Tuesday, July 22, 2008
One bug leads to another
I thought I'd write a quick summary of some bug hunting goodness over the weekend. For some time I have used a custom file loading class that allows me to pass it an array of targets, and URL's and it happily loads them one after another and calls a listener when complete. Its always worked great - until this time.
Within the class I pop an element off the top of the passed in loader array, using the shift() method. The shift() method deletes the element - so when the array is empty everything has been loaded. The problem appeared when I later wanted to reference an item in the original loader array, within my movie, and found it was empty. The problem can be seen in this code clip from the class:
private var loaderArray:Array;
public function init(loadArray:Array):Void
{
this.loaderArray = loadArray;
}
Some of you will recognize the issue immediately - and I would have to had I ever needed to use the original array before. The problem is that arrays are not passed by value. When you set loaderArray = loadArray you are not placing a copy of loadArray into loaderArray. What you are doing is simply setting loaderArray to a memory address, so that it references (points to) the same array. Most of the time this is desirable as it saves on memory, and as long as you're aware of how it works there is no problem. But sometimes you forget... or in this case it just never mattered before.
So, after finally realizing why my original array was getting erased I corrected it by copying the array instead:
private var loaderArray:Array;
public function init(loadArray:Array):Void
{
for(var i = 0; i < loadArray.length; i++){
loaderArray.push(loadArray[i]);
}
}
At least I thought I had corrected it. Anyone see the problem now? The compiler doesn't see anything wrong either. However, loaderArray doesn't get anything pushed to it - because it was never initialized. It's a null object. The compiler doesn't see anything wrong because loaderArray is typed as array, so push is fine. Fixing is as simple as:
private var loaderArray:Array;
public function init(loadArray:Array):Void
{
loaderArray = new Array();
for(var i = 0; i < loadArray.length; i++){
loaderArray.push(loadArray[i]);
}
}
This is only an issue in the AS2 compiler, if you try this in AS3 you'll get a compiler error like so: Error #1009: Cannot access a property or method of a null object reference. telling you exactly what the problem is. You can't use the push method on an object that hasn't been initialized.
squish
Within the class I pop an element off the top of the passed in loader array, using the shift() method. The shift() method deletes the element - so when the array is empty everything has been loaded. The problem appeared when I later wanted to reference an item in the original loader array, within my movie, and found it was empty. The problem can be seen in this code clip from the class:
private var loaderArray:Array;
public function init(loadArray:Array):Void
{
this.loaderArray = loadArray;
}
Some of you will recognize the issue immediately - and I would have to had I ever needed to use the original array before. The problem is that arrays are not passed by value. When you set loaderArray = loadArray you are not placing a copy of loadArray into loaderArray. What you are doing is simply setting loaderArray to a memory address, so that it references (points to) the same array. Most of the time this is desirable as it saves on memory, and as long as you're aware of how it works there is no problem. But sometimes you forget... or in this case it just never mattered before.
So, after finally realizing why my original array was getting erased I corrected it by copying the array instead:
private var loaderArray:Array;
public function init(loadArray:Array):Void
{
for(var i = 0; i < loadArray.length; i++){
loaderArray.push(loadArray[i]);
}
}
At least I thought I had corrected it. Anyone see the problem now? The compiler doesn't see anything wrong either. However, loaderArray doesn't get anything pushed to it - because it was never initialized. It's a null object. The compiler doesn't see anything wrong because loaderArray is typed as array, so push is fine. Fixing is as simple as:
private var loaderArray:Array;
public function init(loadArray:Array):Void
{
loaderArray = new Array();
for(var i = 0; i < loadArray.length; i++){
loaderArray.push(loadArray[i]);
}
}
This is only an issue in the AS2 compiler, if you try this in AS3 you'll get a compiler error like so: Error #1009: Cannot access a property or method of a null object reference. telling you exactly what the problem is. You can't use the push method on an object that hasn't been initialized.
squish
Saturday, July 19, 2008
Sorry bout that...
Sometimes you take a break. I was in the midst of finishing a large project and while it's still not completely finished, it's close enough to take a break. So, expect some Flash content soon. I'm going to try and feature answers / tutorials to the most common questions I see in the Adobe Flash forums.
But since this blog isn't all about Flash - check this out: Off Road Fire Press Release a long time friend, Jeff Idleman of the Iona Group, wrote up a nice press release for ORF. It was tweaked a bit by family and friends and then submitted...
But since this blog isn't all about Flash - check this out: Off Road Fire Press Release a long time friend, Jeff Idleman of the Iona Group, wrote up a nice press release for ORF. It was tweaked a bit by family and friends and then submitted...
Subscribe to:
Posts (Atom)
