Freeing a copied entity

Blitz3D Forums/Blitz3D Programming/Freeing a copied entity

big10p(Posted 2004) [#1]
Hi

I had to revisit an old demo today which seemed to run OK but hung after about 20 mins. I checked the code and the only potential prob I could see was that I created a master entity (cube) which I then copy (copyentity) many times before freeEntity'ing the master cube.

I guess this is a no-no as the copies refer to the same mesh which has now been freed. Or is it OK to do this?

I'm a bit rusty as I haven't done any programming in ages - I've lost my mojo (i.e. enthusiasm) :(


jfk EO-11110(Posted 2004) [#2]
As far as I know the master entity will not be removed as long as there are any copies around. At least that's what I assumed until now. There might be other reasons why it crashes. Lost mojo? Cmon - you just made an Art break.


big10p(Posted 2004) [#3]
Oh, so doing a FreeEntity on the master entity won't have any effect then? Hmmm. Maybe the crash was just Windows throwing on of it's occasional wobblies. The demo in question isn't very complex - not doing any mesh manipulations of anything.

lol. I am on a bit of an art break at the mo as I've just started trying my hand at 3D modeling having found a modeler I can actually use (wings3D - highly recommmended). ;)


ChrML(Posted 2004) [#4]
Add an a place where it says the number of bytes video memory left using AvailVidMem(), and see if it decreases. If you copy an entity, then you have to free all copies, but if you just copy the pointer, you can't.

Works:

mesh = LoadMesh("mesh.x")
mesh2 = CopyMesh(mesh)

FreeEntity mesh
FreeEntity mesh2


Here we just copy the pointers:

mesh = LoadMesh("mesh.x")
mesh2 = mesh

FreeEntity mesh
FreeEntity mesh2 ;THIS WILL CAUSE ACCESS VIOLATION BECAUSE HERE YOU TRIE FREEING THE SAME ENTITY TWICE (THE SAME MEMORY LOCATION)


jfk EO-11110(Posted 2004) [#5]
I think only Mark could answer this question, if a hidden "original entity" is legally kept in memory when it already has copies and then was removed manually.

But personally I think this is trivial, so it might work correctly. Then again - oftenly the trivial bugs are the mean ones.


Floyd(Posted 2004) [#6]
Mark has implicitly answered this in his sample programs that come with Blitz3D.

For example, look at the pick or tex_render programs.
They both create an entity, make many copies and then free the original.


DJWoodgate(Posted 2004) [#7]
I would *guess* that a count is kept of all entities that reference a mesh. Copying an entity that points to a given mesh would increment the count and freeing an entity that points to this mesh would decrement the count. When the count is zero the mesh is freed? In any event I doubt thats the cause of the problem (unless you are not freeing all the copies after you are finished with them). Its not something I have come across anyway. If memory were being chewed up its my experience that every starts running slower and slower. If thats the case then you do have a memory leak somewhere, perhaps you are not freeing a pivot or have a runaway type list.


big10p(Posted 2004) [#8]
Floyd: Well, if Mark's doing it then it must be OK.

Beginning to think it was down to windows having a random crash but if anyone would like to prove me wrong, the demo in question is here:

http://www.blitzbasic.com/codearcs/codearcs.php?code=955