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. Post a comment or leave a trackback: Trackback URL.

3 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;
    }
    }
    }
    }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*