TList memory management

BlitzMax Forums/BlitzMax Beginners Area/TList memory management

why in this code there is a memory leak?

please help-me!
my head explode! ;)

Strict

Type tStruct
	Field n		: Int
	Field Nome	: String
EndType

Type cTest
	Field Lista: TList
	Function Create: cTest(Elements: Int)
		Local t			: cTest = New cTest
		Local NewElement: tStruct
		
		t.Lista = New TList
		
		For Local j: Int = 0 To Elements
			NewElement = New tStruct	
			NewElement.n = j
			NewElement.Nome = "UserName "+j
			ListAddLast(t.Lista, NewElement)
		Next
		Return t
	EndFunction
	
	Method Free()
		ClearList(Lista)
		Lista = Null
	EndMethod
EndType

Global StartMem	: Int = 0
Global ListMem	: Int = 0
Global FreeList	: Int = 0
Global TestList	: cTest

FlushMem()
StartMem = MemAlloced()
Print "************************************************************************************************" 
Print "Start Memory: "+StartMem

TestList = cTest.Create(10000)
FlushMem()
ListMem = MemAlloced()
Print "************************************************************************************************" 
Print "Memory after List Alloced: "+ListMem+" Bytes (Memory Alloced) "+(ListMem-StartMem)+" Bytes"

TestList.Free()
TestList = Null
FlushMem()
FreeList = MemAlloced()
Print "************************************************************************************************" 
Print "Memory After Freeing List and cTest Type: "+FreeList+" Bytes (Differece) "+(FreeList-StartMem)+" Bytes not Released!"

Print "************************************************************************************************" 
End




sorry i forgot...

if you create a list with 20 elements no memory leak, but with
21 elements there is a memory leak!!

ex:

TestList = cTest.Create(20)
no memory leak!

TestList = cTest.Create(21)
400 bytes memory leak!

help!

no Solution/Suggestions ?

well you can get the memory leak down by putting a flushmem in the loop in which it is creating the elements.. but that still leaves a 4000byte gap which i cant fix

I don't think it's a leak, as much as a memory pool issue.

Do you experience this in production code aswell?

yes.

but i think we need more clarity about memory management of blitzMax.

i don't think there is a bug in my test code... but the memory is not freed correctly!

...at this point i don't konw if it's me that i don't have understand the blitzmax memory management or if the blitzmax memory management it's not so clean!

help!