FreeEntity MAV'ing

Blitz3D Forums/Blitz3D Programming/FreeEntity MAV'ing

rgdyman(Posted 2011) [#1]
Hello everyone,

I seem to be missing something very basic here and could use a new set of eyes/brains.

What I am doing is Freeing some Items. The Items are loaded in
and I visually see them.

I have a "Delete Item" button. When I have the Item selected
and press Delete Item the item vanishes without a hitch.

I also have a "New Character" button. This is used to view a
different character.

When the New Character button is pressed I naturally need to
clean out all and any already loaded Items from the Item type.

Using the same exact method I use for Delete Item I attempt
to clean the type up and Free the Item using FreeEntity()

Seems easy? Thats what I thought , but for the life of me I can not
see why I am erroring out. Says Entity Doesnt Exist when I
press New Char button and points right at my FreeEntity(item\itemEN)

Heres the code
;free items
For item.item = Each item

    WriteLog(MainLog,"item en in clean up : " + item\itemEN	)
    FreeEntity item\itemEN
    Delete item 
				
Next

;This is what is in the  Log File
item EN When Loaded and created  : 47067080
item en in clean up : 47067080




My Log file does in fact state the item\itemEN has an ID number.
This only confuses me more :)

Last edited 2011

Last edited 2011


Adam Novagen(Posted 2011) [#2]
I encountered this exact problem in my own code. Here's the kicker: FreeEntity() does indeed delete the entity from memory, BUT for reasons unknown to me, it doesn't make the entity's handle 0. Instead, it changes it to some other integer which means nothing to me. The handle still doesn't point to an entity, though, which means FreeEntity() will of course produce an MAV. My solution was to add a second line that set the entity's handle to 0 immediately after FreeEntity() had been called, then have a line in the manner of:

;Pseudocode here

If Entity\Handle
    FreeEntity(Entity\Handle)
    Entity\Handle = 0
EndIf


That should solve your problem. Just make it so that FreeEntity() is called only in the case of a non-zero handle, and set the handle TO zero when it's freed.


Adam Novagen(Posted 2011) [#3]
Oh, it's also worth noting that, in your example, you're calling WriteLog() BEFORE you're freeing the items. Put WriteLog() after the freeing code and you'll see it records a different number, although still not 0.


rgdyman(Posted 2011) [#4]
Thank you Adam.

This little bugger is stopping cold right at freeentity()
The moment I call FreeEntity I crash and burn.

Doesnt even give me a fighting chance :)

Here is the updated version
Note: I still do have the write log just not in this snippet.

For item.item = Each item
      If item\itemEN > 0
	FreeEntity(item\itemEN)
	item\itemEN=0
      EndIf 
Next


I will re-read what you said Adam, Im hoping I am just not understanding what you said to do.

I assumed If EN > 0 Then FreeEntity then Delete item would clear me up.

Oh while Im here, Pressing the Delete Item event works perfect.

Here is the case:
NOTE: I have a selected item global so the user can select each Item to move around or delete.

For item.item = Each item
						
      If item\itemEN=selecteditem
	   FreeEntity item\itemEN
	   Delete item 							
     EndIf 
						
Next


The above snippet works flawless. I see the Item on the character
I press Delete Item button and the Item vanishes.

I have searched the code looking for an area I may reset the items EN or something I may of just goofed and came up empty. All seems in order.

UPDATE: Ok, It was me and my code the entire time. I managed to free the MeshEN to early. I moved the item deletion above the Mesh deletion
and all seems fine. Im still very confused as to why I was crashing due to the fact the mesh and items have no true connection besides visual.
I do have the items set to a Bone Parent... Thats the only connection.
If I delete the Mesh ( the rig deletes also ) would that of caused a error?

Last edited 2011


Yasha(Posted 2011) [#5]
Are you parenting entities in item objects to other entities? Because FreeEntity will free all of the target entity's children as well; but it won't do anything about the item instance since it doesn't know about those. This means that if children are further along in the item list than their parents, the 3D entity will already have been freed by the time the loop gets around to that item instance.

for reasons unknown to me, it doesn't make the entity's handle 0


The entity no longer has a handle, because it doesn't exist. The memory address in the integer variable now points to empty space.

If you're wondering why the variable wasn't set to zero, it's because only the value of variables is passed to functions. There's no way for the function to reach backwards down the call stack and somehow affect variables in a lower call frame, as it has no way to tell which variable that value came from (and it could easily be more than one - almost certainly). Even if it could, this would be very bad as it has no idea what else you might be using that integer for (because it's just an integer, not strongly-typed to be an entity).

The whole by-value vs by-reference thing can get a little confusing because it looks like type instances and 3D entities are passed directly, but what you're actually passing is the integer address of their block of memory - so you're still only passing a copy of a value, and that value can't be affected outside the function.

If this issue is confusing you heavily, you might want to take a quick holiday in the land of the C programming language: its full support for explicit pointers makes it a great way to learn the difference between passing by value and by reference, which will help your understanding of B3D's memory model enormously as it's basically a cut-down version of the same thing.

Last edited 2011


rgdyman(Posted 2011) [#6]
LOL, sorry double posting.. Yasha posted right when I was editing my post.

Yes, I do have item attached to the Mesh's rig. I was deleting the rig before the item.

Yasha wrote

Are you parenting entities in item objects to other entities? Because FreeEntity will free all of the target entity's children as well; but it won't do anything about the item instance since it doesn't know about those. This means that if children are further along in the item list than their parents, the 3D entity will already have been freed by the time the loop gets around to that item instance.



If I understand correctly, I deleted the Mesh to early and the Parent
was deleted to soon. I hope Im close :)
But, the crash has ended after I deleted the Items BEFORE the Mesh.


Adam Novagen(Posted 2011) [#7]
Oh, something that just hit me. This line:

If item\itemEN > 0

For stability's sake, it should use <> instead of just the > operator. Unless I am severely mistaken, it is actually possible for entity handles to be ANY 32-bit integer, which include negative numbers.

If item\itemEN <> 0


Last edited 2011