Jump to content

#1
gamecreator

gamecreator
  • Members
  • 1,874 posts
I have the following code which crashes as soon as it tries to Release the first tentacle entity.  However, if I switch the for loops to remove the tentacles first, then the rocks, it works fine.  Does anyone see why this could be?  Am I releasing world entities incorrectly?

// REMOVE ROCKS
printf("world entity count: %d\n", world->CountEntities()); // 2231
for(i=world->CountEntities() - 1; i>0; i--)
{
	if(world->GetEntity(i)->GetKeyValue("name").compare(0, 4, "rock") == 0)
	{
		 world->GetEntity(i)->Release();
	 }
}

// REMOVE TENTACLES
printf("world entity count: %d\n", world->CountEntities()); // 2129
for (i=world->CountEntities() - 1; i>0; i--)
{
	if (world->GetEntity(i)->GetKeyValue("name").compare(0, 8, "tentacle") == 0)
	{
		 world->GetEntity(i)->Release();
	 }
}



#2
Josh Klint

Josh Klint
  • Staff
  • 11,135 posts
Could one of those entities be a parent of another?  You might be freeing more than one entity when you do that.

Also, check to make sure GetEntity() isn't returning NULL.

#3
gamecreator

gamecreator
  • Members
  • 1,874 posts
I threw this line in before release
printf("tentacle info: %d, %d\n", i, world->GetEntity(i));
but it isn't NULL.

The tentacle is an animated model with bones and a collision mesh.  The rock is just a model with a collision mesh (no bones).  Neither are parented to anything or by anything otherwise.  The above code happens pretty much right after I load the map.  I'm going through the map entities and replacing them with my own.

#4
Josh Klint

Josh Klint
  • Staff
  • 11,135 posts
When you delete the last entity in the list you are deleting its children, causing more than one entity to get deleted.

However, GetEntity would return NULL if the index is bigger than the entity list.

Another option is to call System::GCSuspend() before the loop and System::GCResume() after it.  This will prevent objects from getting deleted even when their ref count goes to zero, and then the resume call will clean everything up.

#5
gamecreator

gamecreator
  • Members
  • 1,874 posts
Suspend/resume worked.  Thank you.