core icon indicating copy to clipboard operation
core copied to clipboard

gmi_model freeing

Open JaredCrean2 opened this issue 7 years ago • 0 comments

While working on #208, I noticed this line. Combined with this line, it looks like the mesh will free the model when the mesh is destroyed. This is a problem when two meshes use the same model (for example, if one mesh is a copy of another).

The only solution I can think of is to implement reference counting for gmi_model*. This gets a little awkward, though, because of code like this:

apf::Mesh2* loadMdsMesh(const char* modelfile, const char* meshfile)
{
  gmi_model* mdl = gmi_load(modelfile);
  apf::Mesh2* m = loadMdsMesh(model, meshfile);
  return m;
}

I see two possible cases, both with downsides:

  1. gmi_load sets the reference count to 1 when it loads the model, and the mesh constructor also adds 1 to the refcount in its constructor. Now loadMdsMesh needs to decrement the recount before exiting.
  2. gmi_load does not set the refcount, so the mesh constructor sets it to 1. This seems to be the desired behavior. The problem is if a geometric model is not passed to a mesh constructor, its reference count will be zero, causing a problem when gmi_destroy is called.

Overall, I'm not convinced either option is particularly good.

JaredCrean2 avatar Jan 09 '19 18:01 JaredCrean2