core
core copied to clipboard
gmi_model freeing
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:
-
gmi_loadsets the reference count to 1 when it loads the model, and the mesh constructor also adds 1 to the refcount in its constructor. NowloadMdsMeshneeds to decrement the recount before exiting. -
gmi_loaddoes 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 whengmi_destroyis called.
Overall, I'm not convinced either option is particularly good.