Will this cause a memory leak?

Monkey Forums/Monkey Programming/Will this cause a memory leak?

matty(Posted 2011) [#1]
I have a vague understanding of the garbage collector, I understand that as soon as an object goes out of scope it will be set to be collected by the garbage collector.

However - what happens with images and sounds? If I have different levels with a tile based set of images, can I simply load the new tileset directly over the image reference of the old tileset without worrying about freeing the images first?

eg

tile1 = LoadImage("tile1.png")
'tile1 holds reference to image tile1.png
tile1 = LoadImage("tile2.png") 'sometime later in the program assume I call this
'tile1 now holds reference to image tile2.png,...but what happened to tile1.png - will this cause a memory leak or will it be garbage collected?





ziggy(Posted 2011) [#2]
tile1.png is a file. Nothing the free BUT the object with the data associated to this image, wich had only one reference in the variable tile1, as it does not have any more references, it'll be free whenever it is collected. the issue I see here is that you can't be sure when the GC will collect the data, so it is a good idea to manually unload the image before, so you don't have an inefficient VRAM handling.


benmc(Posted 2011) [#3]
Would nulling tile1 prior to a LoadImage help with memory management?


ziggy(Posted 2011) [#4]
No. when you null a variable, you remove a reference to the object the variable is pointing to. It makes no difference. Setting a variable to null just before setting it to anything else has no benefits.


skid(Posted 2011) [#5]
The answer to original answer is no.

See the docs on the Discard method of Image and Sound for more info.