Delete Objects? Delete One Object List?

BlitzMax Forums/BlitzMax Programming/Delete Objects? Delete One Object List?

Yue(Posted April) [#1]
Deleta All Objects.

' Destructor.
	Method DeInit()
	
		Local fuentes:TFuente = Null 
			
		If fuentes.lista:TList <> Null 
				For fuentes:TFuente = EachIn Self.lista:TList 
			
				
				If fuentes.fuente:Int Then 
				
					xFreeFont( fuentes.fuente:Int ) 
					fuentes.fuente:Int = False 
				
				End If 
			
			
			
			Next 
			
			fuentes.lista.Clear()
			fuentes.lista:TList = Null 
		
		
		End If 
	
	
	End Method 







My question is how to delete an object alone, and what I remove from the list where they are stored?

I've thought of a method like this. But I do not know if it's the right one.



       ' Eliminar Fuente.
	Method FreeFont(fuente:TFuente )
	
		
		xFreeFont( Self.fuente:Int ) 
		Self.lista.Remove( fuente:TFuente )
		fuente:TFuente = Null  
	
	
	End Method






fuenteSpace:TFuente = LoadFont("Data/Fuentes/space age.Fuente",22,True )
fuenteSpace.SetFont()
' Hilos.
hilo:THilo = LoadingRes(RES_MENU ) 
hilo.Loading()

fuenteSpace.FreeFont(fuenteSpace:TFuente)





grable(Posted April) [#2]
That is a perfectly valid way to do it. Just know that it has to iterate through the list every time, so if you do this often enough the slowness adds up.

Another way is to store a TLink inside TFuente and then do Link.Remove() instead, which saves you the iteration.
You get TLink from AddFirst() and AddLast(), and they can be used to iterate backwards (or any crazy order you want ;) through a TList as well.

But.. If you expect to add and free many things during a frame (not Fonts i imagine) using an Array is the fastest.


Yue(Posted April) [#3]
@Grable Thanks You. :)

If it works, checked with Count () from the lists to know the items they contain. Now the question is: what is a TLink for?

Some simple example of your


degac(Posted April) [#4]
A TLINK means the 'position' in the list (doesn't mean the 'number' inside, is not related with the order of inserting, and it can change after a sort, adding other items at the top of the list (AddFirst) etc)

It's can be used as a sort of 'connection' between the object and the list.
Of course an object can be stored in many list, a link for each one.

It's quick because you dont' need to 'scan' a list to find the object: useful with many objects and many lists.

Quick example to 'find' immediately a position in the list of an object and of course to remove it from the list (method Remove())




col(Posted April) [#5]
- A TList is made up of TLink(s).
- Each TLink has a 'next link' and a 'previous link' and also the object that you want to store in a TList.
- Usually your own object won't have ( and doesn't need ) the required fields to know where the next and previous objects are in the list, therefore your object is placed inside a TLink.
- This mean that each TLink is a container that holds the object that you 'Add' to the list.
- A TLink can only contain a single object - but of course that object can be anything including other TLists/TMaps etc if you really wanted.


Derron(Posted April) [#6]
Working with "TLinks" has of course a tiny bit of danger contained.

Why?

If your object contains a "link:TLink" and you do this:

obj.link = List.AddLast(obj)

Then you create a circular reference.
obj.link._value is "obj".
-> MyObject(obj.link._value).link._value is still "obj"
-> MyObject(MyObject(obj.link._value).link._value).link._value is still "obj"
-> ...

That is so far no problem, but if you now manipulate the TList somehow _not_ using the official methods (list.Clear()) you might end up creating a memory leak. As "obj" references "link" which references "obj" the GarbageCollector wont think "obj" is safe to get removed from memory while there is no external reference existing (a list or a variable containing "obj").

Do not be afraid, as long as you do not manipulate "TList-fields" it should work flawlessly. But hey, this post might have introduced you to the world of "circular references" at least.


bye
Ron