11 Replies Latest reply on Jan 20, 2010 6:34 AM by allanjard

    addElement causes loader to freeze, but no error

    allanjard Level 1

      Hello all,

       

      I've got a fairly simple little component which extends spark.components.Group. It adds a VGroup instance using addElement(), and then a number of Buttons to the VGroup. The problem that I am encountering is that when I add the buttons using addElement to the VGroup, the preloader for the swf just freezes when it reaches 100%. No errors are reported on screen or in the log file, and the compiler runs cleanly. I've tried this on beta 2 and a release for a week or two ago (4.0.0.13368). I've also tried addChild(), but that does produce an error.

       

      This is basically what I am using:

       

       

      var tmp:spark.components.Button;
      var i:int, iLen:int;
      for ( i=0, iLen=5; i<iLen ; i++ )
      {
           tmp = new spark.components.Button();
           tmp.width = unscaledWidth;
           tmp.height = iHeight;
           tmp.label = i;
           _vGroup.addElement( tmp );
      }
      

       

       

      If I comment out the addElement line, then the swf completes and loads, but obviously my buttons aren't on the stage.

       

      I should also say that if I construct an array of the button elements and then set the mxmlContent property of _vGroup to be that array, it works well. But the documentation suggests that doing this is bad, and that the recommended API of addElement should be used...

       

      Can anyone suggest what I'm doing wrong?

       

      Many thanks,

      Allan

        • 1. Re: addElement causes loader to freeze, but no error
          David_F57 Level 5

          Hi,

           

          You mention preloading so I assume that you are adding these components during the creation of the the display object, are you doing this in the initialisation phase or creation complete phase ?. It may be you have one of those things that just quietly fails, this maybe a bug as things should never fail silently. Could you give a little more info on how/when you are creating the button array.

           

           

          David.

          • 2. Re: addElement causes loader to freeze, but no error
            allanjard Level 1

            Hi David,

             

            Thanks for the reply! yes indeed, these components are being created along with the object. An override of:

             

            - createChildren is used to add the VGroup to the parent (addElement works great here)

            - updateDisplayList is used to add the Buttons to the VGroup - which is the one causing issues.

             

            Is this the wrong approach to take? The only reason I create the buttons in updateDisplayList rather than createChildren is so I can use measuredHeight and width of the component in the button creation.

             

            Regards,

            Allan

            • 3. Re: addElement causes loader to freeze, but no error
              David_F57 Level 5

              Hi,

               

              Where does iHeight come from ?, have you tried putting straight values into the width/height properties to see if thats the issue ?

               

              David.

              • 4. Re: addElement causes loader to freeze, but no error
                allanjard Level 1

                Hi David,

                 

                iHeight is just a local variable (int) which is set to 35. I was using the measuredWidth for the width, but I've just tried that with a static number as well as the height, and it still hits the same problem.

                 

                Regards,

                Allan

                • 5. Re: addElement causes loader to freeze, but no error
                  David_F57 Level 5

                  Hmmm,  this works for me-Is this the type of logic logic you are applying ?

                   

                  ?xml version="1.0" encoding="utf-8"?>

                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                     xmlns:s="library://ns.adobe.com/flex/spark"

                     xmlns:mx="library://ns.adobe.com/flex/mx"

                     creationComplete="application1_creationCompleteHandler(event)">

                  <fx:Script>

                  <![CDATA[

                  import mx.events.FlexEvent;

                   

                  import spark.components.Button;

                  import spark.components.VGroup;

                   

                  private var _vGroup:VGroup = new VGroup();

                  private var tmp:Button;

                   

                  protected function application1_creationCompleteHandler(event:FlexEvent):void

                  {

                   

                  _vGroup.top=10; _vGroup.left=10;_vGroup.width=200; _vGroup.height=300;

                  this.addElement(_vGroup);

                  var i:int = 0;

                  var iLen:int = 10;

                  for ( i=0; i<iLen ; i++ )

                  {

                  tmp = new Button();

                  tmp.width = unscaledWidth;

                  tmp.height = 20;

                  tmp.label = String(i);

                  _vGroup.addElement( tmp );

                  }

                  }

                   

                  ]]>

                  </fx:Script>

                  </s:Application>

                  • 6. Re: addElement causes loader to freeze, but no error
                    allanjard Level 1

                    Hi David,

                     

                    Thanks for following up on this!

                     

                    I tried your code, but mxmlc errors out for me:

                     

                    /Flex/tmp.mxml(14):  Error: Constructor functions must be instance methods.
                    private var tmp:Button = new Button();
                    
                    However, making 'tmp' a local variable for the init complete function allows it to compile for me and run successfully. Here is an example code clip which shows the problem I'm seeing:
                    <?xml version="1.0" encoding="utf-8"?>
                    <s:Application 
                         xmlns:fx="http://ns.adobe.com/mxml/2009" 
                         xmlns:s="library://ns.adobe.com/flex/spark" 
                         xmlns:mx="library://ns.adobe.com/flex/mx" >
                    <fx:Script>
                    <![CDATA[
                    import mx.events.FlexEvent;
                    import spark.components.Button;
                    import spark.components.VGroup;
                     
                    private var _vGroup:VGroup = null;
                     
                    override protected function createChildren():void
                    {
                         super.createChildren();
                         if ( !_vGroup )
                         {
                              _vGroup = new VGroup();
                              addElement( _vGroup );
                         }
                    }
                     
                    override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
                    {
                         super.updateDisplayList(unscaledWidth, unscaledHeight);
                         
                         var tmp:Button;
                         for ( var i:int=0,iLen:int=10 ; i<iLen ; i++ )
                         {
                              tmp = new Button();
                              tmp.width = unscaledWidth;
                              tmp.height = 20;
                              tmp.label = String(i);
                              _vGroup.addElement( tmp );
                         }
                    }
                    ]]>
                    </fx:Script>
                    </s:Application>
                    
                    Hopefully it will do the same for you! Sorry - I should have posted this in the first place...
                    Regards,
                    Allan

                    • 7. Re: addElement causes loader to freeze, but no error
                      David_F57 Level 5

                      Hi,

                       

                      As soon as I saw the code I thought it was going to go into an endless loop on the updatedisplay, but its worse than that as each button is added the display is updated which means your routine is run again...

                       

                      will this do what you want ?

                       

                      <?xml version="1.0" encoding="utf-8"?>

                      <s:Application

                      xmlns:fx="http://ns.adobe.com/mxml/2009"

                      xmlns:s="library://ns.adobe.com/flex/spark"

                      xmlns:mx="library://ns.adobe.com/flex/mx" >

                      <fx:Script>

                      <![CDATA[

                      import mx.events.FlexEvent;

                       

                      import spark.components.Button;

                      import spark.components.VGroup;

                       

                      private var _vGroup:VGroup = null;

                       

                      override protected function createChildren():void

                      {

                      super.createChildren();

                      if ( !_vGroup )

                      {

                      _vGroup = new VGroup();

                      addElement( _vGroup );

                      doButtons();

                      }

                      }

                       

                      protected function doButtons():void

                      {

                      var tmp:Button;

                      var i:int=0;

                      var iLen:int=10;

                      for (i=0; i < iLen ; i++ )

                      {

                      tmp = new Button();

                      tmp.width = unscaledWidth;

                      tmp.height = 20;

                      tmp.label = String(i);

                      _vGroup.addElement( tmp );

                      }

                      }

                      ]]>

                      </fx:Script>

                      </s:Application>

                      • 8. Re: addElement causes loader to freeze, but no error
                        allanjard Level 1

                        Hi David,

                         

                        And as soon as I saw your reply I had a large "DOH" moment.... That makes perfect sense - and yes, your version does the trick very nicely. I didn't think that the unscaledHeight was available at the time of creating the children, but obviously got the wrong end of the stick there.

                         

                        Do you know if there is any infinite loop detection in the Flash player which would catch this kind of behaviour in future?

                         

                        Regards,

                        Allan

                        • 9. Re: addElement causes loader to freeze, but no error
                          David_F57 Level 5

                          Hi

                           

                          Yeah, I think its called the "hey you have no system resource left, thankyou for your understanding and please try again after a reboot" 

                           

                          trouble is this can take some time on a machine with several gig of RAM....

                           

                          flashplayer can somtimes catch loops that make the system unresponsive but for what you were achieving it really would just happily tick away for quite some time as it was using negligible cpu.

                           

                          David.

                          • 10. Re: addElement causes loader to freeze, but no error
                            allanjard Level 1

                            Haha - it's a good point well made :-)

                             

                            Thanks very much for your suggestions in resolving this - I'll be more careful in future!

                             

                            Regards,

                            Allan

                            • 11. Re: addElement causes loader to freeze, but no error
                              allanjard Level 1

                              Hello again,

                               

                              Sorry for dragging this up from the depths, but I've figured out why I was thinking that unscaledWidth / unscaledHeight are unavailable in createChildren(). They are unavailable (or rather they report '0') when a sub-class component has it's size given in a relative value (width=100% for example). When given as an absolute value, the width/height are correctly reported as what is set. I presume there is some calculation that needs to go on in order to calculate the height and width - the problem is, that these two properties are exceptionally useful for actually creating the children!

                               

                              I'm not sure I see a way around this other then to create the children, add them to the display list, and then resize them in the updateDisplayList function - is that the expected interaction?

                               

                              Thanks,

                              Allan