2 Replies Latest reply on Oct 5, 2010 3:12 PM by cean

    Serious memory leak on PollingChannel.as

    OdysseusLevy

       

      I have tracked down (and fixed locally) a serious memory leak in PollingChannel.as. It turns out that PollCommandMessageResponder objects are getting created for every polling message but never getting released because they are getting held in memory by an event listener.  The Flash Builder profiler shows this quite clearly. If you have a client that is on a polling connection it is eventually going to wrong out of memory and crash.

       

      I filed a bug: https://bugs.adobe.com/jira/browse/SDK-27883

       

      The fix is simple -- just cleanup the event listener. The nice thing about this bug is that you don't have to take my word for it -- please just look at PollingChannel.as code and notice at the bottom:

       

       

        public function PollCommandMessageResponder(agent:MessageAgent, msg:IMessage, channel:PollingChannel, log:ILogger)

          {

              super(agent, msg, channel);

              _log = log;

             

              // Track channel connected state.

              // If the channel disconnects while this poll is outstanding, suppress result/fault handling.

             channel.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, channelPropertyChangeHandler);

          }

       

       

      Now look (in vain) to see where that event listener is cleaned up -- it isn't.

       

      My fix is simple -- just release the event listener after it is no longer needed. I do it two places:

       

      Place 1:

       

       

      override protected function resultHandler(msg:IMessage):void

          {     

              var pollingChannel:PollingChannel = channel as PollingChannel;       

             

       

      //

      //VANTOS: This line was added to remove a serious memory leak

      channel.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE, channelPropertyChangeHandler);

       

      Place 2:

       

       

          override protected function statusHandler(msg:IMessage):void

          {       

      //

      //VANTOS: This line was added to remove a serious memory leak

      //

       

      channel.removeEventListener(PropertyChangeEvent.PROPERTY_CHANGE, channelPropertyChangeHandler);

       

       

       

      ************

      My hope is that I get more eyes on this. I've verified that this stops the memory leak, but there might be some subtlety that I missed. If necessary people might want to do their own private patches (like I've done), but really this should be fixed.

       

      By the way I just want to say kudos for having this all open source. It meant that I could fix this on my own, also the code is well written which made it much easier to understand, and the ant script just worked -- I didn't have to monkey around at all which was very satisfying.