Posted by: edsyrett | 22 September 2008

Using Conditional Compilation to detect debug mode

We just fell down a huge hole….

But luckily there was a ladder to get us out.

Have a look at this code:

private function get debug():Boolean { var debug:Boolean = false; // Find out whether or not we are running the debug swf simply by looking // for it in the url. THIS ONLY WORKS FOR Flex2 !!! debug = (this.url.indexOf("-debug.swf") > 0) ? true : false ; // test for Flex 3 by looking for the debug symbols in the app if (debug == false) { debug = getDebugFlag(); } return debug; } private function getDebugFlag():Boolean {     var debugMode:Boolean = false;     var e:Error = new Error();     var s:String = e.getStackTrace();     var i:int = s.indexOf("getDebugFlag");     if (s.charAt(i + 14) == '[')     {         debugMode = true;     }    return debugMode; }

The intent of this code is to tell you whether or not you have a “debug” build, i.e. if you have debug symbols in the swf.

We used to use this to detect non-debug builds and then pull off a trick to embed the SVN release in the filename of the released swf.   The reason for this is that we found that various proxies and caches would hold on to copies of the swfs, and when we did an update everybody wondered why they didn’t see any changes.   Drop me a comment if you’re wondering what I’m on about or you want to know more…

But this code has one real, big-time problem.

getStackTrace() returns null if you run it in the non-debug Flash Player.  And guess what happens when you try to dereference the null value held in s?  In the non-debug Player, you won’t get any error messages – everything will just fail silently.

So this code had to go.   Instead, we’re now using Conditional Compilation.  For ageing C++ hackers like me, this is standard fare, and luckily ActionScript 3 provides this facility.

Here’s our revised function that simply returns a boolean:

private function get debug():Boolean {     var debug:Boolean = false;     CONFIG::debug     {     // If CONFIG::debug is defined as true, this line of         // code gets executed and we overwrite the original value.         debug = true;     }     return debug; }

A bit simpler isn’t it.   The bit inside CONFIG::debug is included if you specify:


in the compiler flags.  So in the flags for Flex Builder, its true, and in our Ant scripts for release builds, it’s false.


I thank you.

And the moral of the story is…

Test your apps in the non-debug version of Flash Player



  1. Thank you for this post.

  2. For a solution that works with Flex3 check out this hack:

  3. Thanks for the Post. Nice Solution!

    In Flex 3 i needed to write 2 doublepoints (::) not one (:)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: