How to check if you’re running a debug swf

There was a brief thread on flexcoders recently about how to check if an swf was built for debugging or for release.  With this check you can build all sorts of debugging diagnostics into your application and then compile an unaffected release build without changing any code.

There is no built in way to perform this check, so putting it modestly, the solution is a bit of a hack.  In a debug swf, the stack trace contains line number information that is absent in a release swf.  To check if you’re in debug mode, all you have to do is throw an error, catch it, and check the stack trace for the square brackets that surround the line numbers.

Here is a little class I wrote that will perform this check.

package com.michaelvandaniker.capabilities
{
    public class SWFCapabilities
    {
        private static var hasDeterminedDebugStatus:Boolean = false;
 
        public static function get isDebug():Boolean
        {
            if(!hasDeterminedDebugStatus)
            {
                try
                {
                    throw new Error();
                }
                catch(e:Error)
                {
                    var stackTrace:String = e.getStackTrace();
                    _isDebug = stackTrace != null && stackTrace.indexOf("[") != -1;
                    hasDeterminedDebugStatus = true;
                    return _isDebug;
                }
            }
            return _isDebug;
        }
        private static var _isDebug:Boolean;
    }
}

I’ve tested this class in the debug and release versions of Flash Player 10.0.12 and 9.0.124.

This entry was posted in Uncategorized and tagged . Bookmark the permalink. Both comments and trackbacks are currently closed.

7 Comments

  1. Posted November 25, 2008 at 5:32 am | Permalink

    Brilliant!

  2. Posted November 25, 2008 at 7:53 am | Permalink

    I agree, quite useful “bit of a hack”

  3. Igor
    Posted April 1, 2009 at 9:09 am | Permalink

    I am usually debugging my apps localy and deploty them on site. in sutch case it’s also possible to check if appication was runned locally.

    package net.panellabs.utils
    {
    public final class ApplicationPath
    {
    public static var isLocal:Boolean;

    public static function init(SWFPath:String):void //ApplicationPath.init(loaderInfo.url);
    {
    if(SWFPath.indexOf(“file:”) > -1)
    {
    isLocal = true;
    }
    else
    {
    isLocal = false;
    }
    }
    }
    }

  4. Posted July 7, 2009 at 2:48 pm | Permalink

    Hi, Thibault Imbert from http://www.bytearray.org redirects me here. I recently have posted a similar solution to this problem without finding this post first, sorry.

    The solution I use is the same but had the idea to directly use new Error() to always have access to getStackTrace without to have to use a try…catch.

    I implement it in my own apps with :
    public static const debug:Boolean = new Error().getStackTrace().search(/:[0-9]+]$/m) > -1;

  5. Posted July 7, 2009 at 2:51 pm | Permalink
  6. judah
    Posted May 11, 2010 at 2:43 pm | Permalink

    Calling:

    isDebugSWF = new Error().getStackTrace().search(/:[0-9]+]$/m) > -1;

    can prevent your swf from loading in the release version of the Flash Player (not debug though). If you need to use it call it after the application has loaded and not in the constructor of your class.

  7. nook
    Posted November 7, 2011 at 10:28 am | Permalink

    Maybe good to clearify how this differ from Capabilities.isDebugger since you use the same terminology (“debug” vs “release”).

    AFAIU, your version checks if swf is running in debug mode, whereas isDebugger checks if swf is running in debug player.

    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/system/Capabilities.html#isDebugger