Add share_ptr to tbox, heat and flow functions
- CMakeLists.txt 1 addition, 1 deletionCMakeLists.txt
- flow/_src/floww.i 13 additions, 24 deletionsflow/_src/floww.i
- flow/src/wAdjoint.cpp 8 additions, 8 deletionsflow/src/wAdjoint.cpp
- flow/src/wAssign.cpp 9 additions, 9 deletionsflow/src/wAssign.cpp
- flow/src/wAssign.h 10 additions, 13 deletionsflow/src/wAssign.h
- flow/src/wF0Ps.cpp 3 additions, 4 deletionsflow/src/wF0Ps.cpp
- flow/src/wF0Ps.h 2 additions, 2 deletionsflow/src/wF0Ps.h
- flow/src/wFreestream.cpp 2 additions, 2 deletionsflow/src/wFreestream.cpp
- flow/src/wFreestream.h 3 additions, 3 deletionsflow/src/wFreestream.h
- flow/src/wMedium.cpp 11 additions, 4 deletionsflow/src/wMedium.cpp
- flow/src/wMedium.h 6 additions, 6 deletionsflow/src/wMedium.h
- flow/src/wNewton.cpp 10 additions, 10 deletionsflow/src/wNewton.cpp
- flow/src/wPicard.cpp 2 additions, 2 deletionsflow/src/wPicard.cpp
- flow/src/wSolver.cpp 5 additions, 5 deletionsflow/src/wSolver.cpp
- heat/_src/heatw.i 4 additions, 0 deletionsheat/_src/heatw.i
- heat/src/wBoundary.cpp 2 additions, 2 deletionsheat/src/wBoundary.cpp
- heat/src/wBoundary.h 3 additions, 3 deletionsheat/src/wBoundary.h
- heat/src/wMedium.cpp 4 additions, 2 deletionsheat/src/wMedium.cpp
- heat/src/wMedium.h 3 additions, 3 deletionsheat/src/wMedium.h
- heat/src/wSolver.cpp 9 additions, 9 deletionsheat/src/wSolver.cpp
-
This commit adds smart pointer support for the function classes in tbox (
Fct...
), heat (CompiledFct...
) and flow (F...
).At this point the code is:
- compiling and running in Release config
- compiling and running in RelWithDebugInfo config, except for flow tests
- compiling but failing at runtime in Debug config, for all tests
I could not identify the cause of the failure at runtime in debug, but this happens since v1.7.1 at least. So, this commit is not responsible for that failure and I will not talk about it.
Regarding the failure at runtime for flow tests, it is triggered by these lines:
initial = Initial(msh, "group", F0PsPhiInf(...)) dirichlet = Dirichlet(msh, "group", F0PsPhiInf(...))
where
Initial
andDirichlet
both derive fromAssign
, andF0PsPhiInf
derives fromF0Ps
. The constructor ofAssign
isAssign(shared_ptr<MshData>, string, shared_ptr<F0Ps>)
.
More specifically, the cause of the failure is an assertion triggered in a SWIG generated function (that explains why it does not fail in release):int SWIG_Python_ConvertPtrAndOwn(PyObject*, void**, swig_type_info*, int, int*) { // ... if (own) { int newmemory = 0; *ptr = SWIG_TypeCast(tc,vptr,&newmemory); if (newmemory == SWIG_CAST_NEW_MEMORY) { assert(own); /* badly formed typemap which will lead to a memory leak - it must set and use own to delete *ptr */ if (own) *own = *own | SWIG_CAST_NEW_MEMORY; } } // ... }
where
int *own
is the last parameter passed toSWIG_Python_ConvertPtrAndOwn
.
After a few tests, I realized that printing*own
resulted in a segfault. Note that several of my classes pass through this bit of code, but never trigger the assertion since*own = 0
. I do not understand what is happening there.What is even more puzzling is that I have other classes, which do exactly the same thing, but with function classes from tbox, and they do not fail. A simple example is the
Freestream(shared_ptr<MshData>, string, shared_ptr<Fct1>)
class, that I instantiate in python asfreestream = Freestream(msh, "group", Fct1C(...))
, whereFct1C
derives fromFct1
.Finally, I should mention that I ran valgrind on both the RelWithDebugInfo (after commenting out the
assert(own)
) and the Release configs, and that it detected no memory leaks.In the end, I have several questions:
- What is happening and why?
- Why do the classes deriving from
flow::F0Ps
cause an issue, while the classes deriving fromtbox::Fct0
do not? - Can we trust valgrind when it says that there is no memory leak (knowing that the comment after the assertion in the SWIG function indicates that there will be a memory leak)?
Additional notes if you got so far without dying:
- This issue https://github.com/swig/swig/issues/731 is still opened in the SWIG repo and is basically the same I run through
- If I change
Assign
to accept the derived classF0PsPhiInf
(instead of the baseF0Ps
), it works fine. I also tried to replaceF0ps
byFct0
, and it runs. - I encountered no issue, nor memory leaks due to the combined use of
shared_ptr
anddirector
, which was my initial fear.
Many thanks in advance.