Function.vector() deleted prematurely when parent goes out of scope (python)
Bug #889021 reported by
Joachim Haga
This bug affects 1 person
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
DOLFIN |
Fix Released
|
High
|
Garth Wells |
Bug Description
If I remove the v._dummy=x line in the function below, I get a segfault when trying to use the returned vector. I can create a self-contained test if necessary, but the problem looks obvious (f gets deleted while its vector data is still in use).
def _as_vector(self, x):
if isinstance(x, GenericVector):
return x
[...]
f = interpolate(x, self.space)
v = f.vector()
v._dummy = f
return v
Changed in dolfin: | |
status: | Confirmed → In Progress |
assignee: | nobody → Garth Wells (garth-wells) |
Changed in dolfin: | |
status: | In Progress → Fix Committed |
Changed in dolfin: | |
status: | Fix Committed → Fix Released |
To post a comment you must log in.
The problem is that Function returns a reference to the vector rather than a shared pointer. The solution are
1. Change Function such that it returns a shared_ptr. This is my preferred option. I don't believe that it will unduly complicate the C++ interface since in typical usage a user can do
solve(A, *u.vector(), b);
in place of
solve(A, u.vector(), b);
2. Add two more functions to Function called vector_ptr() and boiler plate to the SWIG interface to ignore and rename. I don't like this because following this option consistently we'll end up with four functions instead of two throughout the library and we'll always have to remember to hand tweak the SWIG interface to a avoid a memory bug.