9 Replies Latest reply on Jun 24, 2007 12:51 PM by kglad

    movieclip linked to a class

    Psycho_Mantis
      I have a question about the garbage collector:

      I have a movieclip in my library, that is linked to a class. So when I place that class on stage, I see the movieclip.
      Now when I wanted to delete my class, I would delete all event listeners, and make sure there are no references to it. Then I remove it from it's parent. It's a public class, so there will be no vars to delete (right?).

      Now here's the deal:
      in the library, I open the movieclip, which has 10 frames, and in frame 1, I put:

      trace("still here");

      now when I run the program, and delete that class instance, not totally surprisingly, it will keep tracing "still here".
      my question: how do I delete that class instance properly? And can I be sure the class will be deleted when the "still here" is not being traced? Maybe I need to delete the movieclip inside the class? How can I do that when they are in my library and don't have a id?
      Help would be very much appreciated!

      Edit:
      I made a document of the situation :
      download
        • 1. Re: movieclip linked to a class
          kglad Adobe Community Professional & MVP
          you still have a reference in main, nC. not that getting rid of that reference will help the garbage collector. i just think it (the gc) sucks.
          • 2. Re: movieclip linked to a class
            Psycho_Mantis Level 1
            no, nC is gone after the function. It's no variable for the 'Main' class, but just for that one function. When I trace nC in an enterFrame function, it will give an error since nC is undefined. Also, when I set nC to null the problem remains.
            I agree with you about the GC, it doesn't seem too bad but I hate the fact that you can't see if it does its job allright.
            • 3. Re: movieclip linked to a class
              kglad Adobe Community Professional & MVP
              well, i take back what i said about the gc. it does work. but it looks like it only starts to clean when the flash player consumes about 7500 kb.

              and i don't see how you're going to set nC to null from circle.as. i set it to null in main.as where that reference exists:


              • 4. movieclip linked to a class
                Psycho_Mantis Level 1
                Yeah that seems to be it! So I didn't do anything wrong, it's just that the garbage collector didn't sweep yet. I'm glad to hear that. This also seems to be a useful method to see if the garbage collector works! Just put a trace() function in the library item.

                quote:

                Originally posted by: kglad
                and i don't see how you're going to set nC to null from circle.as. i set it to null in main.as where that reference exists:



                Yeah that's what I meant. Just after creating nC, I set it to null. But again, it will disappear anyways.
                • 5. Re: movieclip linked to a class
                  kglad Adobe Community Professional & MVP
                  "But again, it will disappear anyways."

                  i'm not sure what you mean by that. in the context of this thread, your circle instance will not be garbage collected unless all references to it are set to null. so, it won't disappear from memory "anyways".

                  setting it to null after it's created is ok for testing, but is a very poor programming technique.
                  • 6. movieclip linked to a class
                    Psycho_Mantis Level 1
                    quote:

                    Originally posted by: kgladi'm not sure what you mean by that.


                    I mean the reference: If you have a function inside a class, and declare a var in that function, that var will not mean anything to the class itself. It's only usable inside the function you declare it in. That's why I don't set it to null. The reference will disappear anyway.
                    example:

                    function Func() {
                    var a = "hello";
                    trace("a: " + a);
                    }
                    Func();
                    trace("a: " + a);

                    //output:
                    //"a: hello"
                    //"a: "


                    quote:

                    Originally posted by: kgladsetting it to null after it's created is ok for testing, but is a very poor programming technique.


                    why? In my program, I need an instance to be made, that controlls itself. So I delete the reference instantly, and won't have to worry about it anymore.
                    • 7. Re: movieclip linked to a class
                      kglad Adobe Community Professional & MVP
                      you do have to worry about it. that was the point of your testing.

                      first, if you remove all references, you object will be removed (at some imprecise time) by the gc. i don't see much point in creating an object and then immediately readying it for gc.

                      however, if you null your object and create non-weak reference listeners, your object will not be gc'd until the listeners are removed. so, you do have to worry about it and remove the listeners.

                      and using listener removal to trigger gc is what i mean by "...a very poor programming technique".
                      • 8. Re: movieclip linked to a class
                        Psycho_Mantis Level 1
                        ok. Well anyways, thanks for the help. I'm glad I finally got this 'problem' solved. (it wasn't a problem after all :) )
                        • 9. Re: movieclip linked to a class
                          kglad Adobe Community Professional & MVP
                          you're welcome.