title: How to debug a runtime stack underflow error?
body: <p>I'm really struggling to resolve a stack underflow that I'm getting.  The traceback I get at runtime is:</p>
  
  <pre>VerifyError: Error #1024: Stack underflow occurred.
  
  at flash.events::EventDispatcher/dispatchEventFunction()
  at flash.events::EventDispatcher/dispatchEvent()
  at flash.net::URLLoader/onComplete()
  </pre>
  
  <p>This is particularly difficult to debug because when I run in debug mode it does not happen at all.  It only happens when compiled as a release.</p>
  
  <p><strong>Does anyone have any tips on how to debug a Stack Underflow?  Are have a clean explanation of what that means for Flash?</strong></p>
  
  <p>In case it helps, this error is occurring when I click a button whose handler makes an RPC call, which uses a URLLoader, an AsyncToken, and then invokes the set of AsyncResponder instances associated with the AsyncToken.  With some server-side logging as well as some logging hacked into the swf, I know that the UrlLoader is successfully doing and GET'ing a crossdomain.xml file, is correctly processing it (ie: if I wreck it, I get a security error), and is also successfully completing the "load" request (the server sends the data).  The underflow seems to be happening in the Event.COMPLETE listening/handling process (as is, of course, implied by the traceback as well).</p>
  
  <p>mxmlc used = from flex_sdk_4.5.0.20967</p>
  
  <p>Example player (I've tried a few) = 10.2.153.1</p>
  
  <hr>
  
  <p><strong>UPDATE</strong>: My specific problem is solved... but I'm leaving the question as-is since I would like to know how to generally debug such a problem, rather than just getting my specific solution.</p>
  
  <p>In my code I had the following Application definition:</p>
  
  <pre><code>&lt;s:Application height="100%" width="100%"
                                xmlns:fx="http://ns.adobe.com/mxml/2009"
                 xmlns:s="library://ns.adobe.com/flex/spark"
                 xmlns:mx="library://ns.adobe.com/flex/mx"
                 initialize="InitData();"&gt;
  </code></pre>
  
  <p>Note that the code is/was attached to the <code>initialize</code> event.</p>
  
  <p>InitData() and relevant defintions are/were:</p>
  
  <pre><code>import classes.RpcServerProxy;
  public var SP:RpcServerProxy;
  
  public function InitData():void {
      SP = new RpcServerProxy("http://192.168.1.102:1234");
  }
  </code></pre>
  
  <p>When I switched the InitData() call to be on the <code>onCompletion</code> event instead of <code>initialize</code> (thanks J_A_X!), the problem goes away entirely.  What seems to have been happening was that the Event.COMPLETE event handler (onComplete in the stack trace) was using the global SP object.  Something about the release (vs debug) compilation must have been affecting the startup timing of the SP variable initialization.  Moving the handler later to the <code>onCompletion</code> event resolved all issues.</p>
  
  <p>As said above, I would still like to know what tricks/tools are available for debugging initialization issues like this.</p>
  
  <hr>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p><code>applicationComplete</code> seems to be an even better event than <code>creationComplete</code> to put application initialization code.  See <a href="http://www.wietseveenstra.nl/blog/2007/02/understanding-the-flex-application-startup-event-order/">this blog entry</a> for some explanation, and and <a href="http://tv.adobe.com/watch/adc-presents/overview-of-spark-datagrid-in-flex-45/">this video</a> (around 4:25) by an Adobe Tech Evangelist for an example of simple "start of application" data initialization.</p>