Jump to content

Possible: Model::Free() Deletes another model aswell


Ruki
 Share

Recommended Posts

I created a pretty complicated armour system which loads/unloads models and parents them to the entity on the fly. I experienced some crashing errors and I managed to condense my crash into a few simple steps. I spent a whole day targeting this error although it may be me at fault but just wanted to make you aware of the issue just incase. The steps are as follows:

 

// Step #1, load sword to *swordModel, setparent to hand
   // Step #2, load dress to *dressModel, setparent to entity

   // Step #3, unload sword from *swordModel
   // Step #4, load sword to *swordModel, setparent to hand

   // Step #5, unload dress from *dressModel
   // Step #6, load dress to *dressModel, setparent to entity

   // Step #7, unload sword from *swordModel
   // Step #8, load sword to *swordModel, setparent to hand

 

So basically I'm loading a sword and attaching it to hand, loading a dress and attaching it to entity, unloading and loading sword again, unloading dress and loading dress again, and finally unloading and loading sword again. ~80% of the time it CRASHED on step 7 (which I later found out that *swordModel was being deleted by "the bug" and corrupted the pointer. This is the C++ code for those steps:

 

Entity *body = Model::Load( "Models/entity_Cat/body.mdl" ); // Succeeds, model exists
// Step #1, load sword to *swordModel, setparent to hand
Model *swordModel = Model::Load( "Models/DinnerGame/Equipment/Weapons/sword.mdl" );
       swordModel->SetParent( body->FindChild( "Hand_L" ) );
   // Step #2, load dress to *dressModel, setparent to entity
Model *dressModel = Model::Load( "Models/DinnerGame/Equipment/Torso/dress-standard.mdl" );
       dressModel->SetParent( body->FindChild( "Torso" ) );

   // Step #3, unload sword from *swordModel
swordModel->Free();
   swordModel = NULL;
   // Step #4, load sword to *swordModel, setparent to hand
swordModel = Model::Load( "Models/DinnerGame/Equipment/Weapons/sword.mdl" );
       swordModel->SetParent( body->FindChild( "Hand_L" ) );

   // Step #5, unload dress from *dressModel
dressModel->Free();
   dressModel = NULL;
   // Step #6, load dress to *dressModel, setparent to entity
dressModel = Model::Load( "Models/DinnerGame/Equipment/Torso/dress-standard.mdl" );
       dressModel->SetParent( body->FindChild( "Torso" ) );

   // Step #7, unload sword from *swordModel
swordModel->Free();
   swordModel = NULL;
   // Step #8, load sword to *swordModel, setparent to hand
swordModel = Model::Load( "Models/DinnerGame/Equipment/Weapons/sword.mdl" );
       swordModel->SetParent( body->FindChild( "Hand_L" ) );

 

When I set up a breakpoint on #Step7, I found out that during step#5, dressMode->Free() WAS ALSO DELETING MY SWORD MODEL! Shown in the following output:

post-6300-0-46783800-1441241598_thumb.jpg

 

As you can see, the dressModel->Free(); command in step#5 causes swordModel to also be deleted, making my current *swordModel pointer now corrupt. Step#6 executes fine cause it just loads the dressModel back in, but come to Step#7 where I try to unload the swordModel, and it crashes because that pointer is now corrupt.

 

I removed all the parenting commands and it NEVER crashes, it all works fine and as expected. I also tried it on an existing model in Leadwerks and could not replicate the error (one of the FPSarm models, and attaching the same models to its hand bones).

 

My solution:

If i put a swordModel->SetParent( NULL ); just before Step#5, this prevents it from being deleted along with dressModel in step#5 and the code works fine. However, I feel like this is a bit of a 'hackish' solution to a bit of a vagueish bug? I'm really sorry if this not a bug, please feel free to close the topic if you think so. Thanks for your time

Link to comment
Share on other sites

I've never used Free() before, I would use Release() instead.

 

I think the way you are doing it is right setting the swords parent to NULL and wouldn't call it hackish. If you don't it looks like it will get deleted along with the dress model. As long as you keep some sort of reference to the sword so you can delete it too later when you unload the world or otherwise.

trindieprod.png?dl=0spacer.png?dl=0steam-icon.png?dl=0twitter-icon.png?dl=0spacer.png?dl=0
Link to comment
Share on other sites

Without seeing the model and its bone hierarchy, I would assume initially the 'Hand_L' bone is a child/sub-child of the 'Torso' bone. Maybe when making the dress model a child of the 'Torso' bone, it is also making it a parent of the 'Hand_L' bone? It shouldn't do this, but it may explain why releasing one model causes the other to be released. But this is all conjecture.

 

To get an explanation/fix quicker, you will need to provide the models in question and an example demo/code that shows the problem.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

The Entity::Free() function is a bit of legacy design that was never exposed, and in fact forgotten about. I am removing it now.

 

This is the equivalent of calling delete. Use Release instead.

  • Upvote 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...