views:

1451

answers:

4

I Have a problem where I occasionally (i.e. not always) see the below error popup from the Debug Flash Player after launching my app:

Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: http://example.com/myApp.swf cannot load data from localhost:4499.
    at org.mydomain.mypackage::MyClassUsingSocket()
    at MyMainApplicationClass$cinit()
    at global$init()
    at global$init()
    at flash.system::ApplicationDomain/hasDefinition()
    at mx.managers::SystemManager/getDefinitionByName()
    at _MyMainApplicationClass_mx_managers_SystemManager/create()
    at mx.managers::SystemManager/initializeTopLevelWindow()
    at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()

I have some code in this app that tries to connect to localhost:4499 via a Socket, and this error occurs when the socket server is not running and listening for the connections, which is to be expected. What I don't understand, however, is why Flash Player is complaining about unhandled securityErrors when I have try/catch blocks to catch the SecurityErrors when trying to connect the socket (as well as listeners for the SecurityErrorEvents, which this error message doesn't seem to point to, though).

The constructor of the relevant class is below:

/**
* Constructor.
*/
public function MyClassUsingSocket(aHost:String = null, aPort:int = -1):void
{
 super();

 var hostToConnectTo:String = (aHost != null) ? aHost : DEFAULT_HOST;
 var portToConnectTo:int = (aPort != -1) ? aPort : DEFAULT_PORT;


 try
 {
  _out_socket = new Socket();

  // note: the event handlers used below are private functions within the same class
  _out_socket.addEventListener(Event.CONNECT, _socketConnectEventHandler, false,0,true);
  _out_socket.addEventListener(IOErrorEvent.IO_ERROR, _socketIOErrorEventHandler, false,0,true);
  _out_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _socketSecurityErrorEventHandler, false,0,true);

  _out_socket.connect(hostToConnectTo, portToConnectTo);
 }
 catch(e:IOError)
 {
  enabled = false;
 }
 catch(e:SecurityError)
 {
  enabled = false;
 }
}

Any ideas on why this might be occurring? What am I missing?

A: 

This may not be the issue but you're catching SecurityError and its throwing securityError. Maybe try lowercasing the s.

The.Anti.9
Yeah the whole lowercase issue is a bit confusing: "securityError" with lowercase "s" seems to refer to the SecurityErrorEvents (probably since you use the lowercase versions of these to specify event listeners in MXML): http://livedocs.adobe.com/flex/3/langref/flash/net/Socket.html#connect()
hasseg
A: 

Shot in the dark: try catching a generic Error?



        try
        {
                _out_socket = new Socket();

                // note: the event handlers used below are private functions within the same class
                _out_socket.addEventListener(Event.CONNECT, _socketConnectEventHandler, false,0,true);
                _out_socket.addEventListener(IOErrorEvent.IO_ERROR, _socketIOErrorEventHandler, false,0,true);
                _out_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _socketSecurityErrorEventHandler, false,0,true);

                _out_socket.connect(hostToConnectTo, portToConnectTo);
        }
        catch(e:IOError)
        {
                enabled = false;
        }
        catch(e:SecurityError)
        {
                enabled = false;
        }
        catch( e:Error )
        {
                trace( e );
        }
You can also catch(e:*) to be completely paranoid.
aaaidan
A: 

I don't think the errors that show in the dialog box are synchronous. I'm almost certain that they are thrown outside of your code path (during an async network event), so catching them with a try-catch is impossible.

So you'd think that listening for the right event on the right dispatcher would catch the error? I've tried listening to the stage, root, the object (the socket), but nothing suppresses the error.

Yep, it's pretty horrendous. I've been looking for a way to catch these little critters for a while now. The only consolation is that I think it is suppressed in the release version of the player. Still, it's cryptic huh? Not much documentation either (that I've found).

This isn't really an answer. Sorry.

aaaidan
+2  A: 

I too struggled with this for a couple of hours. The solution is to listen for SecurityErrorEvent.SECURITY_ERROR. Apparently the SecurityError is only raised if there isn't such an event handler.