5

我创建了一个非常简单的 SWF 来演示:

package {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.external.ExternalInterface;

    public class FlashIELeak extends MovieClip {

        public function FlashIELeak() {
            addEventListener(Event.ENTER_FRAME, onFrame);
        }

        private function onFrame(e:Event):void {
            ExternalInterface.call("test", null);
        }
    }
}

在 Chrome 中加载它,没问题。记忆或多或少是固定的。

将其加载到 IE 中,内存就会不断上升 - 大约 30k/s。你甚至不需要声明一个测试函数。只需将 swf 嵌入页面中,您就有了泄漏。在我们的项目中,我们传递了一个具有多个属性的对象,而 IE 的泄漏速度为 120k/s。不好。

有人见过这个吗?我找不到与此相关的任何其他帖子。

4

1 回答 1

1

大约 2 年前在著名的俄罗斯博客上有帖子。Alexander Kozlovskij(又名 Fizzer)解释了这个问题。这是他创建的解决方法:

package ru.kozlovskij.external
{
    import flash.external.ExternalInterface;
    /**
     * @author a.kozlovskij
     * @copy flash.external.ExternalInterface
     */
    public final class ExternalInterfaceExtended
    {
        private static const instance :ExternalInterfaceExtended= new ExternalInterfaceExtended();
        private static var methodName :String;

        /**
         * U can't instanciate this class, because it implements a Singletone pattern.
         * @copy flash.external.ExternalInterface.ExternalInterface()
         */
        public function ExternalInterfaceExtended()
        {
            instance && new ExternalInterface();
            methodName = '__flash__addCallback_' + ExternalInterface.objectID;
        }

        private static function updateJS():void
        {
            const jsFunc :String = 'function(){ ' +
                methodName + ' = __flash__addCallback = function(flashObj, methodName)' +
                '{' +
                '    alert("JS: called overridden __flash__addCallback(" + arguments[0] + ", " + arguments[1] + ")");' +
                '    flashObj[methodName] = ' +
                '     (function(methodName)' +
                '     {' +
                '     return function()' +
                '     {' +
                '     this.CallFunction(\'\' + __flash__argumentsToXML(arguments,  + \'\');' +
                '     };' + //dangling semi-colon for IE 6
                '     })(methodName);' + //force re-closure to prevent IE memory leaks
                '};' +
                '}';

            ExternalInterface.call(jsFunc);
        }

        /**
         * Fixed: Mem leaks in native addCallback-js-closure.
         * @copy flash.external.ExternalInterface.addCallback()
         */
        public static function addCallback(functionName :String, closure :Function):void
        {
            updateJS();
            ExternalInterface.addCallback(functionName, closure);
            //ExternalInterface.call(methodName, functionName);
            //ExternalInterface.call('__flash__addCallback_ext', null, functionName);
        }

        /**
         * @copy flash.external.ExternalInterface.call()
         */
        public static function call(functionName :String, ...parameters :Array):*
        {
            parameters.unshift(functionName);
            return ExternalInterface.call.apply(ExternalInterfaceExtended, parameters);
        }

        /**
         * @copy flash.external.ExternalInterface.available
         */
        public static function get available():Boolean
        {
            return ExternalInterface.available;
        }

        /**
         * @copy flash.external.ExternalInterface.objectID
         */
        public static function get objectID():String
        {
            return ExternalInterface.objectID;
        }

        /**
         * @copy flash.external.ExternalInterface.marshallExceptions
         */
        public static function get marshallExceptions():Boolean
        {
            return ExternalInterface.marshallExceptions;
        }

        public static function set marshallExceptions(value :Boolean):void
        {
            ExternalInterface.marshallExceptions = value;
        }
    }
}

因此,您只需要使用 ExternalInterfaceExtended 类而不是常规的 ExternalInterface。

于 2013-12-02T13:45:09.923 回答