4 Replies Latest reply on Nov 26, 2007 7:38 PM by atta707

    Confused about deleting objects

    Aaron Hildebrandt Level 1
      I'm completely new to ActionScript, and I'm having trouble understand how to delete objects.

      If I make a new object, for instance "var shape:Shape = new Shape();", and later I try to delete that object ("delete(shape)"), it spits an error, telling me "Attempt to delete fixed the property drip". This seems to go for every object.

      What's the correct to delete an object? Within the object itself, I tried "delete(this)", which doesn't throw an error, but doesn't seem to work, either.
        • 1. Re: Confused about deleting objects
          atta707 Level 2
          > I'm having trouble understand how to delete objects.

          Actually, you don't need to! There is not concept of object deletion in modern OO programming languages. Memory management has become a responsibility of the run time instead of programmers.

          You might want to read more on garbage collection and it's rules in the Flex docs that come with the Flex builder.

          ATTA

          • 2. Re: Confused about deleting objects
            Aaron Hildebrandt Level 1
            I still don't quite understand. If I understand correctly, I don't delete objects, I just leave them alone, right? Except this doesn't seem to be the case.

            In my main AS, I have the following code:

            public function RainTower() {
            var rainTimer:Timer = new Timer(10);
            rainTimer.addEventListener(TimerEvent.TIMER, DripDrop, false, 0, true);
            rainTimer.start();
            }

            private function DripDrop(event:TimerEvent):void {
            var drip:Raindrop = new Raindrop();
            addChild(drip);
            }

            In a separate file, I have this:

            public function Raindrop() {
            trace("New drop");
            shape.graphics.lineStyle(0, 0x0000FF, 1);
            var offset:Number = 0;
            shape.graphics.drawRect(mouseX+offset, mouseY+offset, 3, 3);
            addChild(shape);
            updateTimer.addEventListener(TimerEvent.TIMER, Update, false, 0, true);
            updateTimer.start();
            }

            public function Update(event:TimerEvent):void {
            shape.x += 1;
            shape.y += 10+_speedY;
            shape.alpha -= 0.05;
            if(shape.alpha < 0) {
            updateTimer.removeEventListener(TimerEvent.TIMER, Update);
            updateTimer.stop();
            }
            }

            It works like you'd expect, except that no matter what you do, things start bogging down after half a minute. How am I supposed to deal with the Raindrop instances that I no longer need? Obviously they're still having a residual effect on things.
            • 3. Re: Confused about deleting objects
              buabco Level 1
              As atta707 suggested, you should read the garbage collector documentation. Objects get destroyed by the garbage collector only once you have stop using them, to achieve this you must eliminate any reference to the object in your code. If any variable or event is still pointing to your object it wont get garbage collected. This means it wont get destroyed.

              By reading your code (Assuming Raindrop is a class file) I can see at least one references that need to be eliminated before garbage collector destroys your object. You've added the raindrop to the stage (So it has a reference there) and there is no place where you removed it from stage. Until you do, the object will not be destroyed.




              • 4. Re: Confused about deleting objects
                atta707 Level 2
                Aaron,

                Is it possible for you to post minimum working app?

                ATTA