简体   繁体   English

在“加载程序”和“加载的”应用程序中类型强制转换失败

[英]Type Coercion failed in 'loader' and 'loaded' applications

My main application swf file is being loaded by a simple loader swf application that is useful to break cache. 我的主应用程序swf文件由一个简单的加载程序swf应用程序加载,该文件对于中断缓存很有用。 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... 它创建Loader实例,创建LoaderContext,并为其提供当前ApplicationDomain和SecurityDomain等。

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. Event.COMPLETE尚未调度,因此不涉及任何处理程序。 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. 显然,Flash Player在这里执行了某些意外行为。 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. 甚至没有任何答案,对此我有些奇怪,因为我发现这是使用小型应用程序加载器时遇到的一个普遍问题。

Spent almost 12 hours trying to solve the problem. 花费了将近12个小时来解决问题。 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. 为了避免上面提供的运行时错误,您需要将单例类编译为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();". 但是我并不需要它,因为我真的很喜欢这种单行单例实例化:“公共静态余量实例:MyClass = new MyClass();”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM