Memory leaks with objects

BlitzMax Forums/BlitzMax Beginners Area/Memory leaks with objects

Otus(Posted 2007) [#1]
In writing a custom list type for improved performance I am constantly finding memory leaks. Could someone please explain why the following test program shows a memory change of 12 bytes?

Framework BRL.Basic

Type MyObject
	Field o:MyObject
End Type

Local o:MyObject
Local mem:Int

GCCollect()
mem=GCMemAlloced()

o = New MyObject		'Create an object
o.o = New MyObject		'Create another object
o.o = Null				'Now the second object should be deleted at GCCollect()
o = Null				'And the first one

GCCollect()
mem = GCMemAlloced() - mem

Print mem


Am I missing something obvious?


H&K(Posted 2007) [#2]
4
0
0
0
0
0
0
0
With repeat forever
So......
I would imagine (And this is a guess), that the first time you ask for an Object, somesort of stucture template is created. Honestly, its not a memory leak unless it goes up and up every cycle.
BUt Im still gonna check back when those clever people comment ;)


Grey Alien(Posted 2007) [#3]
put all the o stuff inside a function. When you declare as local in the main program it doesn't get freed when you set it to null even though you expect it to be. I went through the same thing recently.


Brucey(Posted 2007) [#4]
The objects are still "in scope", so it's very likely that Max is hanging onto the memory (of the variables) incase you were to create a new instance of it.

Trying the same trick from inside a function should result in a different answer.
Not sure about a Local variable declared inside a loop in the main program though...


*(Posted 2007) [#5]
Local variables in the main function just means they cannot be accessed by any function you declare, this can be good for multi declarations but can be a serious pain to debug at later stages.


Otus(Posted 2007) [#6]
So that isn't the cause of my problem, since in the real code everything is done in functions. I have to delve deeper...