5 Replies Latest reply on Jan 27, 2009 7:30 AM by Someonewhere

    Vector.<type> causing casting issue

    Someonewhere
      I have a Flash 10 SWF file using the new Vector collection class to hold interface objects. I load this SWF into my "host" SWF and initialize it, runs great.

      I switch out of this view and the loaded SWF get's destroyed. When I instance a new copy of this Flash 10 SWF file using the Vector collection class a second time, the "push" method of the Vector class throws an exception about type conversion.

      How can this be possible? I just ran it once with the exact same code.
        • 1. Re: Vector.<type> causing casting issue
          Someonewhere Level 1
          Update:

          Adding the following code to the hosting SWF:

          var v:Vector.<ISomeInterface> = new Vector.<ISomeInterface>();

          Fixed the type conversion bug. However, I utilize Vectors of different types in my external SWF that my client won't have access to so can't "pre-declare" the vector.

          Besides that. My client application hosts SWFs written by other developers. What if they write a SWF that utilizes the Vector collection? That means I can't instance their application twice.
          • 2. Re: Vector.&lt;type&gt; causing casting issue
            Someonewhere Level 1
            Here's the error I get on loading the SWF a 2nd time.

            TypeError: Error #1034: Type Coercion failed: cannot convert ArrowSnake::BodySegment@5cfbe51 to ArrowSnake.BodySegment.

            Before adding the Vector declaration in my client I got

            Error #1034: Type Coercion failed: cannot convert CollisionCore.CollisionGraph2D.CollideeHelpers::LineCollidee@60618d1 to CollisionCore.CollisionGraph2D.Interfaces.ICollidee2D.

            So the SWF that loaded the first time retains ownership of the Vector type and since I can't force the garbage collector to get rid of it, I have to go back to the Array collection type I guess.

            Grrrr!
            • 3. Re: Vector.&lt;type&gt; causing casting issue
              Someonewhere Level 1
              Answered my own question. This is an issue caused by Application Domains. When you load an SWF into another SWF, it is loaded into it's own application subdomain. By doing this:

              var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
              _loader.load(new URLRequest(_strURL), ldrContext);

              The SWF you are loading will apply it's "Types" to the current application domain allowing the next load of the SWF access to the types.
              • 4. Re: Vector.&lt;type&gt; causing casting issue
                Level 7

                "Someonewhere" <webforumsuser@macromedia.com> wrote in message
                news:gll2as$omd$1@forums.macromedia.com...
                > Answered my own question. This is an issue caused by Application Domains.
                > When you load an SWF into another SWF, it is loaded into it's own
                > application
                > subdomain. By doing this:
                >
                > var ldrContext:LoaderContext = new LoaderContext(false,
                > ApplicationDomain.currentDomain);
                > _loader.load(new URLRequest(_strURL), ldrContext);
                >
                > The SWF you are loading will apply it's "Types" to the current application
                > domain allowing the next load of the SWF access to the types.

                You may find the information for Q8 also helpful:
                http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf


                • 5. Re: Vector.&lt;type&gt; causing casting issue
                  Someonewhere Level 1
                  Ya, the problem is I haven't seen any documentation saying that the Vector is a singleton class.

                  Under the hood, when you declare a Vector type, Flash Player must create a singleton instantiator for that particular Vector type.