Flex VerifyError: Error #1102

Today the flash .swf I was working on simply stopped working, and I had no idea what was wrong. Fortunately, when using the debug Flash VM, it wrote "VerifyError: Error #1102: Illegal default value for type" or "VerifyError: Error #1102: Unzulässiger Standardwert für Typ" in my case (having the german version).

It took me a bit to find the code causing the problem, but finally I did, and it wasn't that obvious. Take this code for example, looks quite ok, doesn't it?

private function foo(a:SomeType=0):void
// implementation here

Unfortnately, it's wrong and will cause this 'VerfiyError' when running the SWF. The correct version would be

private function foo(a:SomeType=null):void
// implementation here

As it turns out, the ActionScript 3 compiler of Flex has a strange 'feature': You obviously can set default values for parameters as you like.

The problem: When the Flash VM comes to this function and the types don't match, it will simply stop or throw an exception. Setting '0' instead of 'null' is common for C and C++ programmers, but for Actionscript 3, '0' is an int or a Number, and null is the null reference.

The strange part is that the flex compiler doesn't write an error or at least a warning for this and produces a SWF file with corrupt AS3 bytecode.

three comments, already:

Actually using 0 instead of NULL is more common with C++ programmers than with C programmers. The definition is also slightly different:

#ifdef __cplusplus
#define NULL 0
#define NULL ((void*)0)

So basically, 0 and NULL are the same only in C++ (in C they are exchangeable because of the weak type system though). In my experience “pure” C programs normally use NULL, which is the better practice IMHO. After all, a pointer to the memory address “0” is something completely different than an integer with the value “0”.
Michi - 15 10 09 - 14:40

One of these mistakes you do not do anymore when also working with Java and/or C# ;)
Tazo - 15 10 09 - 14:43

I don’t like that strict type checking in Java (though I normally use “NULL”;). Just got used to the weakness of the C-checking from the very beginning of programming.
Brainsaw (link) - 19 10 09 - 07:29

Remember personal info?
Email (optional):
URL (optional):
Enter "layered" (antispam):
Comment:Emoticons / Textile

  ( Register your username / Log in )

Notify: Yes, send me email when someone replies.  

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Note: If you type in your email adress above, it will be visible to other visitors, although it will be hidden for bots using javaScript.