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

Return a Function in C++

+2 votes

Dear all,

I tried to polish my C++ code by returning a function. However, something's wrong since I get an EXEC_BAD_ACCESS error.

Here's my code:

    // MAIN FILE
    auto u = linearElastic(mesh, pts, lambda);

    File vtk_file(path + "displacement.pvd", "compressed");
    vtk_file << u; // <========= EXEC BAD ACCESS

and the linearElastic function:

// Vector space
Elasticity::Form_a::TestSpace V(mesh);

// Solution
Function u(V);

// Solver
PETScLUSolver solver;

// Solve
solver.solve(A, *(u.vector()), b);

return u;

Now, since the code compiled I thought a Function could be assigned, but it seems this is not the case. Maybe the underlying .vector() is uninitialized,

What's the correct way of returning a function? Right now I have an additional reference parameter in the function, but it's not very neat.

Thanks!

asked Jul 10, 2015 by senseiwa FEniCS User (2,620 points)

1 Answer

+2 votes
 
Best answer

Try wrapping the TestSpace and Function in shared pointers so that they aren't disposed at undesirable points.

answered Jul 11, 2015 by Charles FEniCS User (4,220 points)
selected Jul 13, 2015 by senseiwa

Thanks! It worked flawlessly!

Glad to hear!

...