Question About Classes Monkey Damping

Monkey Forums/Monkey Programming/Question About Classes Monkey Damping

hardcoal(Posted 2012) [#1]
When I create a Class Inside a function

Example:

Function CreateImage:Image_Class()
local TempImage:Image_Class= New Image_Class

Return TempImage
End function

After using the function does the TempImage Object Still Exsists
in the memory or the Monkey Engine damps it?


muddy_shoes(Posted 2012) [#2]
If you have a reference to the object then it won't be collected as garbage. In other words: if you can access it then it won't be collected.


hardcoal(Posted 2012) [#3]
if its local inside a function doesn't it mean you cannot access it from outside?


Shinkiro1(Posted 2012) [#4]
I assume you use this function like this:

Local image:Image_Class = CreateImage()


Now image has the reference to this object.
If you would just call CreateImage() then it would be garbage collected because there is no reference to it anymore.


slenkar(Posted 2012) [#5]
yeah it all depends what you do with the image once its returned from the createimage function


NoOdle(Posted 2012) [#6]
the TempImage object exists inside the function. You are then returning it so it still exists. If you don't collect the return value from the function it would then be released, and a new one allocated on the next function call. Once scope is lost it is removed by gc I guess. Hopefully that makes sense as I'm quite drunk and it took 3 edits and I'm still not happy with it :P


hardcoal(Posted 2012) [#7]
lol noodle thanks for the effort.

what really im asking is..
when im calling the function hunderds of times and the function
each time creates a new class would it overwrite the previous created class
or would it create an overload for the memory..


Samah(Posted 2012) [#8]
You don't create classes at runtime, you create instances of them. So I assume you mean "when an object is created, will it reuse the memory address of the last instance".

Short answer: Why does it matter?
Long answer: Unlikely, as there's no guarantee that garbage collection will occur between the deferencing of the previous object and the allocation of the new one. Even if you're SURE that the GC has fired and has collected that object, the memory could already have been reused by another object. Remember that the memory used by the heap has already been allocated, so "reusing it" will give you no speed advantage.