Need help trying to write a function to clear data

BlitzMax Forums/BlitzMax Beginners Area/Need help trying to write a function to clear data

Jaquio1319(Posted 2007) [#1]
'The menu class handles all created menu's in the game
Type TMenu

	''''''''''''''''''''''''''''''''''''''''''''''''''
	'List Globals
	''''''''''''''''''''''''''''''''''''''''''''''''''
	
	Global MenuCollection:TList = New TList

	''''''''''''''''''''''''''''''''''''''''''''''''''
	'Basic Fields
	''''''''''''''''''''''''''''''''''''''''''''''''''
	
	Field X:Int
	Field Y:Int
	Field Name:String
	Field Index:Byte
	
	''''''''''''''''''''''''''''''''''''''''''''''''''
	'Status Fields
	''''''''''''''''''''''''''''''''''''''''''''''''''
	
	Field Active:Byte
	Field DoDraw:Byte
	Field DoAlpha:Byte
	Field DoColor:Byte
	Field MouseOver:Byte
	Field MouseClicked:Byte
	
	''''''''''''''''''''''''''''''''''''''''''''''''''
	'GFX Fields
	''''''''''''''''''''''''''''''''''''''''''''''''''
	
	Field DrawAlpha:Byte
	Field DrawRed:Byte
	Field DrawGreen:Byte
	Field DrawBlue:Byte
	
	''''''''''''''''''''''''''''''''''''''''''''''''''
	'List Fields
	''''''''''''''''''''''''''''''''''''''''''''''''''
	
	Field EntityCollection:TList = New TList
	Field ImageList:TList = New TList
	Field ButtonList:TList = New TList
	Field AudioList:TList = New TList
	
	''''''''''''''''''''''''''''''''''''''''''''''''''
	'Basic Static Functions
	''''''''''''''''''''''''''''''''''''''''''''''''''
	
	'Creates a menu and returns the handle to the provided variable
	Function Create:TMenu(MenuName:String, MenuX:Int = 0, MenuY:Int = 0, MenuIndex:Int = -1, MenuActive:Byte = 1)
		
		Local TempMenuHandle:TMenu = New TMenu
		
		TempMenuHandle.Name = MenuName
		TempMenuHandle.X = MenuX
		TempMenuHandle.Y = MenuY
		
		If MenuIndex <= -1
			TempMenuHandle.Index = 0
		Else
			TempMenuHandle.Index = MenuIndex
		EndIf
		
		If MenuActive <> 1
			TempMenuHandle.Active = 0
		Else
			TempMenuHandle.Active = 1
		End If
		
		TMenu.MenuCollection.AddLast(TempMenuHandle)
		
		Return TempMenuHandle
		
	End Function
	
	''''''''''''''''''''''''''''''''''''''''''''''''''
	'List Static Functions
	''''''''''''''''''''''''''''''''''''''''''''''''''
	
	'Clears the list of menus. Set delete data to true if you want to kill all current menu objects.
	Function ClearMenuList(DelData:Byte = 0)
		
		If DelData = 1
			For Local TempMenuHandle:TMenu = EachIn TMenu.MenuCollection
				TMenu.MenuCollection.Remove(TempMenuHandle)
				TempMenuHandle = Null
			Next
			
			Return
		Else
			TMenu.MenuCollection.Clear()
			
			Return
		End If
		
	End Function
	
End Type


I need help developing the "ClearMenuList()" function i made. Looking at the other data in the type, would i have to go through and delete each list inside TMenu or will making the TMenu variable null also kill those lists completely? I want to make sure all data involving menu entities and audio objects are removed from memory. And I am not sure if I even wrote the MenuCollection list deletion code right...

Edit: Now that I think about it... this wouldnt work would it? I mean am I right to think that im just killing TempMenuHandle and not the object itself? I mean the object will stay in memory if any other variable anywhere in the program is "looking" at it right?


GfK(Posted 2007) [#2]
As far as I can see, your ClearMenuList function contains two different ways of doing the exact same thing.

Nulling TMenu will cause GC to clean up everything else below it *unless* other references to the data exist.


Jaquio1319(Posted 2007) [#3]
Well as you can see in the code the TMenu created will also be returned. Is there a way to find out which other variables contain that data? Because not only will the variable they specify get referenced to it, they can also make other variables reference it by saying local mytmenu2:tmenu = mytmenu granted mytmenu was provited when tmenu.create was called.