A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Object removal question

  1. #1
    Member
    Join Date
    Jun 2006
    Location
    Croatia
    Posts
    90

    Question Object removal question

    Hi,

    I have question regarding removing instantiated objects. Let's say i have a main class in which i instantiate a custom video player (or whatever) class:

    Code:
    ...
    var videoPlayer = new VideoPlayer(...);
    addChild(videoPlayer);
    ...
    And inside VideoPlayer class i have of course dozens of objects (movie clips, sprites representing different parts of VideoPlayer) and dozens of event listeners attached to them.

    My question is, if i want to remove this VideoPlayer class from my main class is it enough to just do:

    Code:
    removeChild(videoPlayer);
    videoPlayer = null;
    Or should i make sure first to destroy the same way all the objects inside VideoPlayer class before doing this removal in main class. What is the best practice here regarding memory performance?

    From my experience, if i don't destroy for example event listeners from my VideoPlayer class and just remove VideoPlayer class the way described they still get fired with null exception messages etc. This is something that's bugging me for a while so i would really like to know what's best thing to do here?

    Thanks in advance,
    Best regards

  2. #2
    rabid_Delineator AttackRabbit's Avatar
    Join Date
    Dec 2003
    Location
    Orlando, Florida
    Posts
    481
    In 90% of cases your code should work. if you want to get super technical , you should have a kill() or shutdown() method you call before you remove it from the display list.The kill or shutdown would remove any event listeners inside VidPlayer(), set any reference variables to any other class objects to null , stop any timers or enterframes , etc. I wouldnt so much worry about having the VidPlayer class remove all of its children, just any that could be problematic.

    Once your shutdown process is complete , i would dispatch an Event to the parent class , to then remove the caller from the display list and set its value to null , just like you have it.

  3. #3
    Member
    Join Date
    Jun 2006
    Location
    Croatia
    Posts
    90
    Quote Originally Posted by AttackRabbit View Post
    In 90% of cases your code should work. if you want to get super technical , you should have a kill() or shutdown() method you call before you remove it from the display list.The kill or shutdown would remove any event listeners inside VidPlayer(), set any reference variables to any other class objects to null , stop any timers or enterframes , etc. I wouldnt so much worry about having the VidPlayer class remove all of its children, just any that could be problematic.

    Once your shutdown process is complete , i would dispatch an Event to the parent class , to then remove the caller from the display list and set its value to null , just like you have it.
    Awesome, that's just was i was thinking about and was actually doing it but wasn't sure if that's the right way Thanks for your time and answer AttackRabbit.

    Cheers

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