A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Proper way to remove objects. Memory leak??

  1. #1
    Game Developer
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    292

    Exclamation Proper way to remove objects. Memory leak??

    Hey all,

    I am making a flash game. The game starts off running fast and smooth, however 15 minutes into the game it's noticibly slower. I worry that I am causing memory leaks by not removing objects from my game properly. I understand the whole garbage collection idea, but I still need confirmation.

    Here is what i do in my game :

    I have a main game class. It controls everything. Within that class I create a new "enemy".

    Code:
    myEnemy = new Enemy(var1, var2, etc)
    addChild(myEnemy)
    Eventually this enemy dies and I want to remove it from the screen and from memory.

    Within the Enemy class I have this :

    Code:
    if(enemyDied) {
        // enemy is now dead
        referenceToMainGameClass.removeEnemy(this);
        delete this;
    }
    And within my main game class I have this :

    Code:
    public function removeEnemy(obj:Enemy):void {
         removeChild(obj);
    }
    Is that the proper way? From my understanding "delete" will delete the object from memory (or at least flag it for deletion for the garbage collector) and the removeChild removes it from the display list.

    Also, I do the delete from within the class I am deleting... I'm guessing I should move that within the removeEnemy function in the main game class? (after remove child)

    Any advice is greatly appreciated!

    Ryan

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    http://www.gskinner.com/blog/archive...source_ma.html

    Make sure you're clearing event listeners as well and removing all references to the object. You can trace out System.totalMemory to get a feel for where your ram usage is over time.

  3. #3
    Game Developer
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    292
    Thanks for the reply.

    I read over the link you sent me. Makes sense.

    One more question... I used the "delete" from within the instance of the class itself. Does that work? Or does the parent have to delete the child?

  4. #4
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    delete is an operator so you can use it from anywhere on pretty much any object.

  5. #5
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    to clarify, delete is used for telling dynamic classes and purely dynamic Object types to de-reference the property being deleted. It doesn't work and will throw an error when used on properties of sealed classes. It is really not a very useful way to control your memory leaks. It's useful when you have a keyed Object that has lots and lots of keys that need to be "spliced" out, like if you're extending Object to provide some kind of data tree or stack. But it's no more useful than splicing the item in question out of an array that happens to be holding it, for memory purposes: You still need to make sure that all other living references to the item are de-referenced (set null), and that no listeners remain on the item, or from the item itself onto a permanent feature of the UI (like the stage, for instance. An enterframe listener from the item that targets the stage is going to keep the item from ever being swept from memory).

  6. #6
    Game Developer
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    292
    Just curious, is there any way for me to tell if an object has any references to it remaining? (just like there is a way for a parent to see all it's children).

  7. #7
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Another thread on this forum a few days back pointed out that the ENTERFRAME listener, can sustain an object even if the listener is weak. So make sure to removeListener the enterframe if they have one.

    I really wish there was a more surefire way to delete something in AS3. My current game won't mind it very much, but I am planning on porting an AS2 project that uses a lot of custom particle effects and that will SURELY need robust object deletion.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center