Destroy an Object

Monkey Forums/Monkey Beginners/Destroy an Object

Moerin(Posted 2014) [#1]
Hi,

I try to code a game with a bouncy ball and an use of Box2D port for monkey.

I try to operate a reset feature to set my object at his original position.
So i have thought to delete my object and recreate it. Since Monkey have a garbage collector, i thought to set my object to the NULL value and recall the new operator on it.

The problem is that my previous object is still here with my new object.

How i manage it.



Here's my full code :




dawlane(Posted 2014) [#2]
The Garbage collector only gets call at specific times. Though you have NULLed an object, it still exists until the GC is called. If it's only the one object why delete it and try to create a new one when it's more efficient just to re-use the object by resetting it's values.


Moerin(Posted 2014) [#3]
Because i don't figure out how to reset an object value create with box2D. I have rode that the regular way is to detroy your object, i can do that with c++ and Java but not with Monkey.


MikeHart(Posted 2014) [#4]
Just store a NULL inside that field where you stored the bodx2D object in and it will be collected.

EDIT: Ah, I saw you use diddy and I didn't study your code troughly. Could be that diddy holds the box2D object somewhere too.


Moerin(Posted 2014) [#5]
Yeah, i have forgot to mention that i'm using diddy. Ok, but i'm at the begining of my project. Do you think if i change my framework for fantom i'll be able to find an issue?


Gerry Quinn(Posted 2014) [#6]
Something else remembers that object too and is still drawing it.

It makes no difference whether the garbage collector has been called yet or not, it only gets called for objects that *everything* has forgotten about.

If you could have deleted it like in C++, you'd probably get a crash instead when the other thing tried to use it. In a GC language, it just carries on existing.

Just go through your code and think about the life cycle of objects and what things get to know about them.


MikeHart(Posted 2014) [#7]
@Moerin

I don't know if you will find an issue. If you are happy with Diddy, I see no reason to change it.


AdamRedwoods(Posted 2014) [#8]
Monkey's port of Box2D does *NOT* collect objects. you must explicitly Destroy() them. monkey's port is based of the flash port, so you can use that documentation:
http://www.box2dflash.org/docs/2.0.2/manual#Memory_Management

_world.DestroyBody(_ballObject)


this is primarily because Monkey does not use implicit destructors.


MikeHart(Posted 2014) [#9]
You are right Adam, forgot about that totally. I do the same in the box2D interface of fantomEngine.


Moerin(Posted 2014) [#10]
It's work now. Thanks to each of you for your help.