11 Replies Latest reply: Aug 19, 2014 11:54 AM by anthonycoffee RSS

    Problem with PopUp Instances

    BhaskerChari Community Member

      Hi,

       

      I am facing one very peculiar problem it seems with the PopUp's in Flex...I will try to explain in detail....

       

       

      I have built one component which is a combination of several components....as shown below....

       

      All my components contains repeaters used with in it...Now what I am doing is I am opening the Comparison.mxml component with in a popup using the

      PopUpManager class....as shown below..

       

      compPopUp = new Comparison();
      PopUpManager.addPopUp(compPopUp , Application.application as DisplayObject, true);
      //compPopUp = Comparison(PopUpManager.createPopUp(Application.application as DisplayObject,Comparison,true));

       

      I have tried both PopUpManager.addPopUp() method and also PopUpManager.createPopUp() method....and I am removing my PopUp as PopUpManager.removePopUp(this); in my removePopUp() function....

       

      Comparison.mxml (My Main component....)

       

      <mx:VBox>

           <comp:Component1  />

           <comp:Component2  />

           <comp:Component3  />

           <comp:Component4  />

           <comp:Component5  />

           <comp:Component6  />

           <comp:Component7  />

      </mx:VBox>

       

      So my problem now is I am opening my main component in a PopUp and after closing if I again try to open the PopUp the previous instance of my popUp is also being rendered and created...in the sense all the data(i.e; my dataproviders which are ArrayCollections I am using) are not emptied and still retain the values even after removing the PopUp and opening it again...

       

      I tried to debug and found that the previous eventListeners and the data of my previous PopUp instance still remains and are not released from memory once I close the PopUp...

       

      Say after I close my popup once and again if I try to open the PopUp the previous instance still remains in addition to my current instnce event listeners and data...

       

      What I am expecting is once I close my popUp component all the references to the previous instance should be freed from memory and only the daa and eventListeners for the current instance should exist but it's not happening...so why...??

      It seems to be a possible bug with PopUpManager class....

       

      Can you please tell any one how can I overcome this problem..???

       

       

      What I tried to figure out this problem is I tried to put an Alert in one funtcion and tried to open the PopUp ...So I got one Alert message and if I try to close the PopUp and open it again I got two alerts..and againg if I close and open it shows 3 alerts....and so on it goes....like that...

       

      And it is taking much much time to open and load my data in the PopUp for the subsequent  opening and closing of my PopUp ...

       

      Have any one encountered this problem earliar...???

       

      Thanks in advance,

      Bhasker Chari

        • 1. Re: Problem with PopUp Instances
          rootsounds Community Member

          I can't tell from the sample provide and assume that you know better, but the first step is to make sure that you don't have that comPopUp variable hanging around.

           

          Second is to make sure that you remove the listeners on close of the popup. Lingering listeners can stand in the way of garbage collection.

          • 2. Re: Problem with PopUp Instances
            BhaskerChari Community Member

            Hi rootsounds,

             

            Ok thanks for the quick reply ...I will try your suggestions.....I can remove event listeners but what about the variables and dataprovider ArrayCollections thery are not being cleared up...

             

             

            Thanks,

            Bhasker Chari

            • 3. Re: Problem with PopUp Instances
              BhaskerChari Community Member

              Hi rootsounds,

               

              I have tried the popUpInstance to be scoped local to the function ...earliar I declared that value external to the function ....but not successfull still the same problem...

               

              Any other thoughts on this...???

               

               

              Thanks,

              Bhasker Chari

              • 4. Re: Problem with PopUp Instances
                rootsounds Community Member

                Something might be holding on to it. You can always try to force it with the delete operator.

                • 5. Re: Problem with PopUp Instances
                  Sebastien V. Community Member

                  Where are the createPopUp() and removePopUp() methods called from? Where are the dataProvider ArrayCollections stored?

                   

                  Also you should probably check your event listeners. When you remove the pop up you should also remove every event listener it has (with the removeEventListener() method).

                   

                  Last idea, if you want the pop up to be removed from memory the comPopUp variable must be set to null (as for any variable refering to the pop up instance).

                  • 6. Re: Problem with PopUp Instances
                    BhaskerChari Community Member

                    Hi Sebastien V,

                     

                    I am using the below function to open popUp ...I am calling this function from another mxml file...

                     

                    private function comparePlans(result:*):void
                       {
                        var planCompPopUp:PlanComparisonPopUp = new PlanComparisonPopUp();
                        //PopUpManager.addPopUp(planCompPopUp, Application.application as DisplayObject, true);
                        planCompPopUp = PlanComparisonPopUp(PopUpManager.createPopUp(Application.application as DisplayObject,PlanComparisonPopUp,true));
                       
                        planCompPopUp.dataProvider = new BAllBenefits().getBenefitTypeData(result as BAllBenefits);
                        PopUpManager.centerPopUp(planCompPopUp);
                       }

                     

                     

                    And my component file which is being opened in PopUp is as below:

                     


                    PlanComparisonPopUp.mxml (My Main component....)


                    <mx:VBox>

                         <comp:Component1  />

                         <comp:Component2  />

                         <comp:Component3  />

                         <comp:Component4  />

                         <comp:Component5  />

                         <comp:Component6  />

                         <comp:Component7  />

                    </mx:VBox>

                     

                    So I am talking about the dataproviders and ArrayCollection in my components Component1 , Component2, Component3 etc..; So when I try to debug after closing the PopUp and opening it again at the first instance the values should be null since these have not yet been instantiated...but it is showing me the previous value pouplated when I opened the PopUp first time...

                     

                    I am having the event listeners from withing the components  Component1 , Component2, Component3 etc..;  but how do I remove them when I am trying to close the PopUp which is opened from other file...???

                     

                    Also I scoped my planCompPopUp instance local to the function so there should be no question of setting to null as you mentioned in your last point...

                     

                    Although I had set it to null when I first decalred the instance as global but no use...the problem still persists...

                     

                     

                    Thanks & Regards,

                    Bhaske Chari

                    • 7. Re: Problem with PopUp Instances
                      Sebastien V. Community Member

                      Ok, first the following lines are useless (I guess it comes from a previous use of the addPopUp() method):

                       

                      var planCompPopUp:PlanComparisonPopUp = new PlanComparisonPopUp();
                      //PopUpManager.addPopUp(planCompPopUp, Application.application as  DisplayObject, true);

                       

                      But this should not change anything.

                       

                      Are you able to check the result object that is passed to the method in debug mode, to ensure that the ArrayCollection you use as the dataProvider of your components are empty?

                       

                      Do you ever use the removePopUp() method and if yes, how do you get a reference on the pop up component since you are using a method local variable?

                      • 8. Re: Problem with PopUp Instances
                        BhaskerChari Community Member

                        Hi Sebastien V,

                         

                        I am able to debug and when I am trying to see the variables(my data) which were declared in another components they are not null and they show the previous values....

                         

                        Also we can close the PopUp for an instance variable method local variable? We can always close the PopUp even when variable is declared locally or global by using this keyword as PopUpManager.removePopUp(this);

                         

                        Sebastien is there any way to remove all the event listeners associated with my custom component  to remove when I close PopUp..???

                         

                        Thanks,

                        Bhasker Chari

                        • 9. Re: Problem with PopUp Instances
                          Sebastien V. Community Member

                          Yes you can close the pop up with PopUpManager.removePopUp(this); as long as this call is made in the pop up. Otherwise you need a reference on the pop up.I'm more used to using PopUpManager.removePopUp( myPopUp ); this is why I asked.

                           

                          If the event listeners are only declared in the pop up, this should not be a problem. It would have been one if you had event listeners on the pop up, but declared in the main. For instance myPopUp.addEventListener( ... ); should be removed with a myPopUp.removeEventListener( ... ); before the pop up is closed, so that the pop up can be garbage collected.

                           

                          For the data problem, I think it does not come from the way you pop/remove your pop up but from the data you send to it. From the code you posted, the pop up data comes from a result:* object that you cast in BAllBenefits. What you need to do is to check this BAllBenefits object content, and see what it contains. The createPopUp method is supposed to instanciate the PlanComparisonPopUp class, so unless the dataProvider is stored in a static variable inside this PlanComparisonPopUp, the previous data should not be recovered unless it is being passed to the pop up in this BAllBenefits object.

                           

                          Another possibility is be that the planCompPopUp.dataProvider = new  BAllBenefits().getBenefitTypeData(result as BAllBenefits); call somehow recovers the previous data.

                           

                          So, what you need to do:

                          - Put a breakpoint in the comparePlans() method and check what the result /  BAllBenefits object contains.

                          - Put a breakpoint in the BAllBenefits dataprovider setter and check what is being sent after the new  BAllBenefits().getBenefitTypeData(result as BAllBenefits) call.

                          - Check how the BAllBenefits contructor is coded, and its getBenefitTypeData( method )

                           

                          Hope this helps.

                          • 10. Re: Problem with PopUp Instances
                            BhaskerChari Community Member

                            Hi Sebastien V and root sounds,

                             

                            Thanks for your support and help...Infact the problem is with the event listeners itself ...but the problem here to be addresses is how I can remove the eventListeners in all my sub components when my main component which is opened in a PopUp window is closed...

                             

                            However I can remove eventListeners which are registered directly in my main component which can be done simply by using removeEventListeners in my removePopUp() method...But what about various eventListeners that are in my sub components...how can I remove them when I close the popup in removePopUp() method since these eventListeners are not directly decalred in my main component..but in my subcomponents...

                             

                            So I did the following in my main component.....

                             

                            Added an eventListener on preinitialize event of my main component:

                            private function onPreInitialize():void
                               {
                               
                                systemManager.addEventListener("removeAllPlanCompListeners", removeEventListeners);
                               }

                             

                            private function removeEventListeners(event:Event):void
                               {
                                systemManager.removeEventListener(CoverageRateAmountChangeEvent.COVERAGERATEAMOUNT_CHANGE EVENT, amountChangeEventHandler);
                                Application.application.systemManager.removeEventListener("removeAllPlanCompListeners", removeEventListeners);
                                this.removeEventListener("planElementLayOutComplete",planElementRepeatEndHandler);
                                this.removeEventListener("planProvisionLayOutComplete",planProvisionRepeatEndHandler);
                                this.removeEventListener("benefitPlanDetailsLayOutComplete",benefitPlanDetailsRepeatEndHa ndler);
                               }

                             

                            private function removePopUp():void
                               {
                                systemManager.dispatchEvent(new Event("removeAllPlanCompListeners"));
                                PopUpManager.removePopUp(this);
                               }

                             

                            So all the above 3 functions I have written in my main component....In my removeEventListeners() method I am removing the eventListeners that are defined in my main component...

                             

                            So my next step is to remove the eventListeners in my sub components...So what I am doing is I am dispatching an event (removeAllPlanCompListeners) when my popup is closed in removePopUp() method ...with respect to systemManager and listening for this event in all my subcomponents....and I am removing all the eventListeners defined in the sub component in the removeEventListeners handler and I am also removing the "removeAllPlanCompListeners" with in the same handler so in this way I have managed to remove all my listeners in my custom component including all the subcomponents...and my problem got resolved...

                             

                             

                            Thanks Once Again,

                             

                            Bhasker Chari

                            • 11. Re: Problem with PopUp Instances
                              anthonycoffee Community Member

                              Please release the whole working code so others can use this. Pretty please.

                              Thanks,

                              Anthony