Hi,
I think the problem is, as already pointed out, the scope and I think, as already pointed out, that it can be solved using shared pointers. However, use it for the FunctionSpace created inside the function and not for the Function itself. Perhaps you should use a shared pointer for the mesh created inside the function as well, but this seems to work:
#include "cG1.h" // element = FiniteElement("Lagrange", triangle, 1)
#include <dolfin.h>
using namespace dolfin;
void function(Function& u);
int main()
{
UnitSquareMesh mesh(10, 10);
cG1::FunctionSpace V(mesh);
Function u(V);
dolfin::info("%d", u.function_space()->dim());
function(u);
dolfin::info("%d", u.function_space()->dim());
return 0;
}
void function(Function& u)
{
UnitSquareMesh mesh(20, 20);
//cG1::FunctionSpace V(mesh);
boost::shared_ptr<cG1::FunctionSpace> V( new cG1::FunctionSpace(mesh));
Function v(V);
dolfin::info("%d", v.function_space()->dim());
u = v;
}