Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 714921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:07:45+00:00 2026-05-14T05:07:45+00:00

Can someone post an example of as3 code (specifically event listener included) that would

  • 0

Can someone post an example of as3 code (specifically event listener included) that would be a simple example of something that could leak memory… also hopefully could you post a solution to the problem shown?

The question is: What is a simple example of leaking memory in an AS3 event listener and how can you solve it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T05:07:45+00:00Added an answer on May 14, 2026 at 5:07 am
    public class MySprite extends Sprite {
    
        public function MySprite() {
            if(stage) {
                init();
            } else {
                addEventListener(Event.ADDED_TO_STAGE,init);
            }
        } 
    
        private function init(e:Event = null):void {
            stage.addEventListener(Event.RESIZE,handleStageResize);
        }
    
        private function handleStageResize(e:Event):void {
            //  do some processing here.
        }
    
    }
    

    Somewhere else:

    var mySprite:MySprite = new MySprite();
    someHolder.addChild(mySprite);
    

    Now, if at some later point you remove mySprite, it’ll still hang around in memory, because it has added itself (or a reference to itself) to the stage in the init() method.

    In this scenario, the best way to avoid this could be removing the listener added to the stage when mySprite is removed from the display list.

        private function init(e:Event = null):void {
            addEventListener(Event.REMOVED_FROM_STAGE,cleanUp);
            stage.addEventListener(Event.RESIZE,handleStageResize);
    
        }
    
        private function cleanUp(e:Event):void {
           stage.removeEventListener(Event.RESIZE,handleStageResize); 
        }
    

    I’m sure other people will tell you to use weak references when adding the listener to the stage, but you should remove your listeners anyway. If you don’t, when you remove mySprite from the display list and have no other refs to it, will be eligible for GC and will eventually be wiped away from memory. But until that happens, the code in handleStageResize() will continue to execute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone post an example of how I get an instance variable value that
Can someone post the simplest thread example for Delphi 2010 that for example puts
Can someone post code or point to a working example of a php script
Can someone post a simple example of using named pipes in Bash on Linux?
The Flex 3 docs are fairly basic. Can someone post a more complete example
Can someone post any simple explanation of cache aware algorithms? There are lot of
Perhaps I'm having a Post-Ballmer-Peak Moment . I'm hoping that someone can help point
Can someone post the simplest example on how to use controlTextDidChange with a text
Can someone please post an example on how to create a many to many
Can someone post a working example of the Enterprise Application Data block, to call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.