gamecreator

World Entity Release

5 posts in this topic

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();

}

}

0

Share this post


Link to post

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.

0

Share this post


Link to post

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.

0

Share this post


Link to post

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.

0

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Create Your Account

Sign in

Already have an account? Sign in here.


Sign In Now