Object TLIst is destroyed by itself?

BlitzMax Forums/BlitzMax Beginners Area/Object TLIst is destroyed by itself?

Yue(Posted 2016) [#1]
'=================================================================
' |* Proyecto		: Airport				*|
' |* Sitio Web		: http://www.Iris3DGames.ga	        *|
' |* Programador   	: Yue Rexie.				*|
'=================================================================
' |* Fichero 		: TCamara.bmx				*|
' |* Notas         	: Objeto Camara.			*|
'=================================================================


' - Objetos.
Local camara:TCamara = Null 


' - Tipo TCamara.
'===============
Type TCamara

	Field camara:Int 
	Field lente:Int 
    Field padre:Int 
	
	
	Global lista:TList ' Where destroyed?
    
	' - Constructor.	
	Function Init_TCamara:TCamara( padre:Int = False )
	
		Local camara:TCamara = New TCamara 
	
	    numero:Int = numero:Int + 1	
		camara.padre:Int     = padre:Int 
		camara.camara:Int    = xCreateCamera( camara.padre:Int )  


        If Not  ( lista:TList ) lista:TList = New TList
        lista.AddLast( camara:TCamara ) 
		
       
		Return ( camara:TCamara ) 

    End Function

    Method ColorCielo( rojo:Byte = 12 , verde:Byte = 183, azul:Byte = 242 ) 

		xCameraClsColor ( Self.camara:Int, rojo:Byte, verde:Byte, azul:Byte ) 

    End Method 
     

    ' Destructor.
    Function DeInit_TCamaras()


			For Local camaras:TCamara = EachIn camaras.lista:TList 
			
			
				If ( camaras.padre:Int ) Then 
				
				
					xEntityParent( camaras.camara:Int, False ) 
				
				End If 
				
				
				If ( camaras.camara:Int ) Then 
				
					
					xFreeEntity ( camaras.camara:Int ) 
				
				End If 
			
			Next

		
			camaras.lista.Clear()

    End Function
    


End Type 



grable(Posted 2016) [#2]
Any globally scoped object is destroyed when you exit the process.
Though at that point it doesnt matter that much as the OS will reclaim whatever memory allocated anyway.

If its important to you, set it to Null in DeInit.

Your code has a few bugs btw, you should probably start using Strict or SuperStrict, it will help you catch those errors quicker.

A tip: Try not to shadow variables too much, especially if your using them within the same scope.


Yue(Posted 2016) [#3]
@grable Thanks You.


I would like you to help me identify those bugs, because apparently here works well, what little I'm doing here looks good.
In such a case this programming with regard to objects is very new to me, and it is not the same in Blitz3D.




grable(Posted 2016) [#4]
Well, it works in non-strict mode, because scoping is relaxed and it doesnt complain about undefined variables (it just creates new ones as Int).
Ive marked those places with <-- which you should review, all of them catched by Strict and SuperStrict if you choose to use either of them.


You dont have to use strict-mode though, but i highly recommend it, as it will make it MUCH easier to debug and it also will catch stupid mistakes like typos.

And you should check up on scoping as well, heres a crude example demonstrating different scopes and shadowing of variables:
Global A:Int = 1

Type Test
 	Global A:Int = 2

	Function TestFunc()
		Print A
	EndFunction

	Method TestMeth()
		Local A:Int = 3
		Print A
	EndMethod
EndType

Print A
Local t:Test = New Test
t.TestFunc
t.TestMeth



Brucey(Posted 2016) [#5]
Here's some reading material :

https://en.wikipedia.org/wiki/Scope_(computer_science)
https://en.wikipedia.org/wiki/Variable_shadowing

I also recommend you add SuperStrict to the top of your code, as it encourages you to think more about what you are doing.


Yue(Posted 2016) [#6]
@Brucey Thanks You for Info :)

Ok, SuperStrcit.
It forces me to declare everything before being used.





File Init.
SuperStrict 
' - Importar módulo Xors3D
Import xorsteam.xors3d
xKey("074P6-kglOy-7DWEA-gCrv4-z6RS8")

' - Includes Generales.
Include "../Generales/Includes.bmx"

' - Init TG3D.
TG3D.Init_TG3D()

sol:TLuz		= TLuz.Init_TLuz:TLuz() 
camara:Tcamara 	= TCamara.Init_TCamara:TCamara()
camara.ColorCielo()
columna:TMalla = TMalla.Init_TMalla:TMalla("Data/Columna.yue")


xRotateEntity   ( sol.luz:Int, 90, 0, 0 ) 
xPositionEntity ( columna.malla:Int, 0, -50, 200 ) 

Local cubo:Int = xCreateCube() 
xPositionEntity cubo:Int, 0, -3, 7


' - Bucle Principal.
While  xKeyHit(XKEY_ESCAPE:Int) = False

		xUpdateWorld(1.0)
	
	    xTurnEntity ( columna.malla, 0.0, 1.0, 0.0 ) 
		xRenderWorld(1.0)
		xText ( 10, 10, "FPS : " + xGetFPS() )
		xFlip()

Wend 

' - Liberando Memoria.
TCamara.DeInit_TCamaras()
TLuz.DeInit_TLuces()
TMalla.DeInit_TMallas()

' - Fin del Programa.
xClearWorld()
xDestroyRenderWindow() 
End





Yue(Posted 2016) [#7]
I think it's better less a little. Do you think this is correct ?.