This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Using adapt for MeshFunction

+2 votes

Hi Everyone, I have a C++ code and up until recently the following code snippet worked:
...
previously created
dolfin::Mesh mesh
std::shared_ptr<dolfin::MeshFunction> pFunction
...

dolfin::CellFunction f(mesh, true);
mesh = adapt(mesh, f);
pFunction = adapt(pFunction, make_shared(mesh));

This used to do one level of uniform refinement of the mesh and then adapt the MeshFunction accordingly. Now however I get a segfault when I try to write the adapted function to a pvd file.
Has something changed recently or am I just not using adapt correctly? What I want is to replace the current mesh and MeshFunction with the adapted versions. I'm using the dev version of dolfin from perhaps two weeks ago.

Thanks,
Dave

closed with the note: Issue satisfactorily resolved
asked Apr 20, 2014 by david.bernstein FEniCS User (2,000 points)
closed Apr 27, 2014 by david.bernstein

Sorry, the stars in front of pFunction didn't make it through...

Hi Dave,

Could you please format your C++ code appropriately and post a minimal running example?

Thanks Marie. When I tried to reproduce the problem I found that it occurs only when the shared_ptr to the mesh that is passed into adapt goes out of scope relative to the MeshFunction which is being adapted. The confusing thing is that the mesh itself stays in scope, it's just the pointer (which is created just to call adapt) is gone.

I guess the issue here for me is that the overloaded version of adapt in this case takes only a pointer to the mesh and not a reference. [And just for the record, I don't actually think this used to work before because in a previous version of my application I was using mesh pointers instead of meshes (not an issue to switch back)].

So I guess the moral of the story is be careful when using std::make_shared to create pointers to pass into dolfin functions...

...