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.
6 Comments
Brilliant!
I agree, quite useful “bit of a hack”
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;
}
}
}
}
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;
Sorry, my post : http://www.tekool.net/blog/2009/06/27/identifying-a-debug-or-release-swf-file-at-runtime/
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.