This site requires JavaScript, please enable it in your browser!
Greenfoot back
won0c
won0c wrote ...

2012/9/25

Does removeObject () method is called, the object is released from memory?

won0c won0c

2012/9/25

#
The object are only invisible on the world? or the object reference is to be set null to be released from memory ASAP? I'd like to the mechanism of the removeObject () method. please...
danpost danpost

2012/9/25

#
If you have a reference to the object that is removed from the world, it is still in memory. You can still get values from its instance fields and call methods within its class on that object (provided these methods do not require the object to be in the world). Also, you can re-insert the object back into the world using its reference name. The only thing that is lost, is its location at the time it was removed, unless you saved them before removing it. If there are no references to the object, it is sent to the garbage collector.
won0c won0c

2012/9/25

#
then, when addObject(new Actor()) is called, when is the Actor instance released from memory??
iau iau

2012/9/25

#
Unless your Actor does something special, the only reference to the Actor you create is the one held by the World. So once you remove that Actor from the World, it is a candidate for release from memory. After that, you can't tell whether your Actor is still in memory or not. It may be removed from memory immediately if something else needs the space. If your program has plenty of memory for all the objects you create, your Actor may never be removed (it would be a waste of effort to remove it). Because you can't call any of its methods, you can't tell whether it is still there or not.
won0c won0c

2012/9/25

#
How could I remove the Actor object from the World without any reference? If I inserted the object by calling addObject(new Actor()), there is no reference to the object, so I think it’s impossible to remove that from world... right?
danpost danpost

2012/9/25

#
To put it the other way, as long as you have a reference to the object or the object is in the world, it will not be removed from memory. Once both of these conditions are not true, it will be assigned for garbage collecting. If you did not yet have a reference to it and you need one before it is removed, you can pass 'this' to a method or assign 'this' to a field of another class (or to a static field in any class; even the same class) to keep a reference to it. So, to answer your initial question, the only thing that 'removeObject(Object obj)' may do as far as the object staying in memory or not, is run a check to see if it can be removed and flag it for garbage collection, if the proper conditions are met. Once it is flagged for garbage collecting, it is lost as far the your project is concerned and it is really of no matter whether it is still there or not (as your project no longer has any ties to it and cannot access it).
davmac davmac

2012/9/25

#
If I inserted the object by calling addObject(new Actor()), there is no reference to the object, so I think it’s impossible to remove that from world... right?
Wrong; the world maintains a reference to it, and you could obtain a reference to it using the collision checking methods (for instance), at which point you could remove it from the world.
danpost danpost

2012/9/25

#
What davmac said is correct. Also, the object could remove itself (when given conditions are true) since its act method gives it a reference: 'this'. For example: if you had an instance variable for an object to count down the cycles of existence 'int remainingCycles = 500;' you could have the following at the end of the act method
remainingCycles = remainingCycles - 1;
if (remainingCycles == 0) getWorld().removeObject(this);
to remove the object after it lives in the world for 500 cycles.
won0c won0c

2012/9/26

#
Thanks a lot !
You need to login to post a reply.