Concern about Free Entity

Blitz3D Forums/Blitz3D Programming/Concern about Free Entity

Yue(Posted 2014) [#1]
Cube% = CreateCube()

Cube% = 0



What happens to the cube ?, is released from memory?


GfK(Posted 2014) [#2]
No - it's still there, you just don't have reference to it any more.

Cube = CreateCube()

FreeEntity(Cube)
Cube = 0


The last line isn't really necessary, but if you don't zero the variable it will still contain a pointer to where the cube entity was stored. So it's good practice to use FreeEntity, then zero the variable.


Yue(Posted 2014) [#3]
okay thanks. Another question is, if I delete an object, for example a cube that is the parent of another cube, the variable of the second cube is released from memory?

Cube1% = CreateCube()
Cube2% = CreateCube(Cube1%)


FreeEntity Cube1%
Cube1% = 0
FreeEntity Cube2%
Cube2% = 0



RemiD(Posted 2014) [#4]
You confuse "delete an object" with delete an entity (=free an entity)

delete an object is :
t.Thing = Object.Thing(Handle)
delete(t)

delete an entity is :
freeentity(t\Mesh)
zero the reference is (after the entity has been deleted) :
t\Mesh = 0

if you delete an entity (=free an entity), all childs are deleted too.
http://www.blitzbasic.com/b3ddocs/command.php?name=FreeEntity&ref=3d_a-z


_PJ_(Posted 2014) [#5]
The handles to entities are just variables. The values they hold can be pointer references.







If using Type object fields to track entities, it is important to Free the entities prior to deleting their respective object instances.
Function DestroyAlien(Instance.Alien)
FreeEntity Instance\Entity_Handle_Field%
Instance\Entity_Handle_Field%=0 ;This isn't necessary since the memory will be cleared and this variable will no longer be accessable anyway.
Delete Instance
End Function



Yue(Posted 2014) [#6]
Ok, I have always something to learn.

My problem is this.

Type MeshCube1
  Field cube%
end type

Type MeshCube2
 
 Field cube%
end type


function NewCube.MeshCube%(father%=0)
   
  Cube.MeshCube = New Cube
  Cube.MeshCube\cube% = CreateCube(father%)

  return Cube.MeshCube%
end function

function NewCube2.MeshCube2%(father%=0)
   
  Cube.MeshCube = New Cube
  Cube.MeshCube\cube% = CreateCube(father%)

  return Cube.MeshCube%
end function


function DeleteCube%()

  Cube.MeshCube = new MeshCube1

  for Cube.MeshCube1 = each MeshCube1

  if Cube\cube% freeentity Cube\cube%
  Cube\Cube% = 0
  if Cube.MeshCube1% <> null delete Cube.MeshCube

  next 

end Function

function DeleteCube2%()

  Cube.MeshCube2 = new MeshCube

  for Cube.MeshCube2 = each MeshCube

  if Cube\cube% freeentity Cube\cube%
  Cube\Cube% = 0
  if Cube.MeshCube2 <> null delete Cube.MeshCube

  next 

end Function


Local Cube1.MeshCube1 = NewCube()
Local Cube2.MeshCube2 = NewCube(Cube1\cube%)


Repeat

renderworld
Flip

until Keyhit(1)
; ERROR HERE.
DeleteCube%()
DeleteCube2%()


I think the mistake is that the second cube is a child of the first, but not as solucinar to the end of the program does not send me the MAV. any suggestions?


Matty(Posted 2014) [#7]
You havent defined a type "MeshCube" ... only MeshCube1 and MeshCube2


Yue(Posted 2014) [#8]
Sorry, the correct code is as follows where an error, which is apparently due to a bucket of objects this son of a cube is created at the end of the application.
Graphics3D 800, 600, 32,2


Type MeshCube1
	Field cube%
End Type

Type MeshCube2
	
	Field cube%
End Type


Function NewCube.MeshCube1(father%=0)
	
	Cube.MeshCube1= New MeshCube1
	Cube.MeshCube1\cube = CreateCube(father%)
	
	Return Cube.MeshCube1
End Function

Function NewCube2.MeshCube2(Pivot%)
	
	Cube.MeshCube2 = New MeshCube2
	Cube.MeshCube2\cube = CreateCube(Pivot%)
	
	Return Cube.MeshCube2
End Function


Function DeleteCube%()
	
	Cube.MeshCube1 = New MeshCube1
	
	For Cube.MeshCube1 = Each MeshCube1
		
		If Cube\cube% FreeEntity Cube\cube%
			;Cube\Cube% = 0
			If Cube.MeshCube1 <> Null Delete Cube.MeshCube1
				
			Next 
			
End Function

Function DeleteCube2%()
	
	Cube.MeshCube2 = New MeshCube2
	
	For Cube.MeshCube2 = Each MeshCube2
		
		If Cube\cube% FreeEntity Cube\cube%
			;Cube\Cube% = 0
			If Cube.MeshCube2 <> Null Delete Cube.MeshCube2
				
			Next 
			
End Function


Local Cube1.MeshCube1 = NewCube()
Local Cube2.MeshCube2 = NewCube2(Cube1\cube%)


Repeat
	
	RenderWorld
	Flip
	
Until KeyHit(1)
; ERROR HERE.
DeleteCube%()
DeleteCube2%()



Matty(Posted 2014) [#9]
In your delete functions remove the first statement where you create an empty meshcube1/2 (it is pointless)

The reason it crashes is because when you free an entity it also frees any child entities...
So when it gets to deletecube2 the child entity no longer exists....


Yue(Posted 2014) [#10]
Is that not all objects have a father. For example what happens is I think a window object, a panel object, and this panel will put some controls on children, but is optional, these controls may be sons of the window or any other object. (Groups), and I'm talking more specifically BlitzPlus. So how is optional not know how to do.


Yue(Posted 2014) [#11]
ok, possible solution and not whether it is the right thing.

Function DeleteCube%()
	
	Cube.MeshCube1 = New MeshCube1
	
	For Cube.MeshCube1 = Each MeshCube1
		Cube\Cube% = 0 ; >>> Fix
		If Cube\cube% FreeEntity Cube\cube%
			
			If Cube.MeshCube1 <> Null Delete Cube.MeshCube1
				
			Next 
			
End Function

Function DeleteCube2%()
	
	Cube.MeshCube2 = New MeshCube2
	
	For Cube.MeshCube2 = Each MeshCube2
		Cube\Cube% = 0 ; Fix
		If Cube\cube%  FreeEntity Cube\cube%
			
			If Cube.MeshCube2 <> Null Delete Cube.MeshCube2
				
			Next 
			
End Function




Matty(Posted 2014) [#12]
Sorry but that just looks completely wrong....


RemiD(Posted 2014) [#13]
Your code looks unnecessarily complicated to not do much... what are you trying to do ?


Yue(Posted 2014) [#14]
I'm really confused and from my point of view this should work correctly.


Function DeleteCube2%()
	
	Cube.MeshCube2 = New MeshCube2
	
	For Cube.MeshCube2 = Each MeshCube2
		Cube\Cube% = 0 ; Fix
		If Cube\cube%  FreeEntity Cube\cube%
			
			If Cube.MeshCube2 <> Null Delete Cube.MeshCube2
				
			Next 
			
End Function



Well before removing the child object exists and verify that the message is the message returns.



The code is just one example of the flow of object creation and subsequent disposal at the end of the program, and if no such instruction executed after changing screen resolution, I think it would have a memory leak.

Create object with a child.
Remove parent object.
Remove Object Son.


Yue(Posted 2014) [#15]
Ok, ok, first removed the children.

Graphics3D 800, 600, 32,2


Type MeshCube1
	Field cube%
End Type

Type MeshCube2
	
	Field cube%
End Type


Function NewCube.MeshCube1(father%=0)
	
	Cube.MeshCube1= New MeshCube1
	Cube.MeshCube1\cube = CreateCube(father%)
	
	Return Cube.MeshCube1
End Function

Function NewCube2.MeshCube2(Pivot%)
	
	Cube.MeshCube2 = New MeshCube2
	Cube.MeshCube2\cube = CreateCube(Pivot%)
	
	Return Cube.MeshCube2
End Function


Function DeleteCube%()
	
	Cube.MeshCube1 = New MeshCube1
	
	For Cube.MeshCube1 = Each MeshCube1
		
		If Cube\cube% FreeEntity Cube\cube%
			;Cube\Cube% = 0
			If Cube.MeshCube1 <> Null Delete Cube.MeshCube1
				
			Next 
			
End Function

Function DeleteCube2%()
	
	Cube.MeshCube2 = New MeshCube2
	
	For Cube.MeshCube2 = Each MeshCube2
		
		If Cube\cube% FreeEntity Cube\cube%
			;Cube\Cube% = 0
			If Cube.MeshCube2 <> Null Delete Cube.MeshCube2
				
			Next 
			
End Function


Local Cube1.MeshCube1 = NewCube()
Local Cube2.MeshCube2 = NewCube2(Cube1\cube%)


Repeat
	
	RenderWorld
	Flip
	
Until KeyHit(1)
; Fix Error
DeleteCube2%() ; Child Delete First
DeleteCube%()




Matty(Posted 2014) [#16]
Ok first thing....since you obviously didnt listen to my earlier comment....

In your delete function. ...there is no reason to create a new mesh cube instance. ...it is a complete waste.

Secondly....a better way would be to store the child parent relationship in a type as well as that way you know if there is a child or not.


Yue(Posted 2014) [#17]
Matty, are you trying to help but the translator of google does not help me much.
What I mean is that I can do this ??



I do appreciate a small example.


Matty(Posted 2014) [#18]

Function DeleteCube%()
	
	Cube.MeshCube1 = New MeshCube1 ;THIS LINE IS COMPLETELY USELESS - REMOVE IT - IT DOES NOTHING!!!
	
	For Cube.MeshCube1 = Each MeshCube1
		
		If Cube\cube% FreeEntity Cube\cube%
		If Cube\pivot% FreeEntity Cube\pivot%
			
			;Cube\Cube% = 0
		If Cube.MeshCube1 <> Null Delete Cube.MeshCube1
				
	Next 
			
End Function



Yue(Posted 2014) [#19]
Ah ok, it is interesting that this is not necessary.
Now my problem is that if I try to remove the two objects the application crashes.
Any suggestions.