Question

My main application swf file is being loaded by a simple loader swf application that is useful to break cache. In both applications I would like to have access to a singleton. I will provide you with an example to reproduce the bug. So here's our singleton:

package {
    public class SingletonTest {
        public static const instance:SingletonTest = new SingletonTest();
        public function SingletonTest() { /* Nothing here. */ }
        public function test():void {
            trace("SingletonTest!");
        }
    }
}

In constructors of both loader and main classes I call:

SingletonTest.instance.test();

That is made in order to be sure that my singleton class code will be included in both applications. I won't provide you with loader code but it's very simple. It creates Loader instance, it creates LoaderContext supplying it with both current ApplicationDomain and SecurityDomain, etc...

But when I launch my loader application I get following error:

Main Thread (Suspended: TypeError: Error #1034: Type Coercion failed: 
cannot convert SingletonTest@47a3091 to SingletonTest.) 
    SingletonTest$cinit 

I get this error right after main application was loaded with loader. Event.COMPLETE is not dispatched yet so no handlers involved. I spent a lot of time trying to find something about application or security domains but it seems like it's not the issue because what I found next is really strange. If instead:

public static const instance:SingletonTest = new SingletonTest();

I will write:

private static var _instance:SingletonTest;
public static function get instance():SingletonTest {
    if (_instance == null) _instance = new SingletonTest();
    return _instance;
}

Then there will be no such error and everything will be fine. Obviously flash player is performing some unexpected behavior here. There's very few information on this issue out there. Most of people get this error because of missing loader context but as I said before here it's not the case. Some discussions on this are even left without any answers which is strange to me because I find this to be quite a common problem to be encountered when using small applcation loader.

Was it helpful?

Solution

Spent almost 12 hours trying to solve the problem. At last found the solution. In order to avoid runtime error which I provided above you need to compile your singleton class into swc-library. That's it.

In my case that singleton was providing global access to logging and only option was to switch from singleton pattern to static class. But I didn't want it very much because I really like this one-line singleton instantiation: "public static cosnt instance:MyClass = new MyClass();".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top