Skip to content
Snippets Groups Projects
Commit 40171e0c authored by Adrien Crovato's avatar Adrien Crovato
Browse files

Add share_ptr to tbox, heat and flow functions

parent 327edc7b
No related branches found
Tags v2.2.2
1 merge request!66v2.2.0 - Adjoint flow and cleaning of tbox
Pipeline #1130 passed
Showing with 110 additions and 112 deletions
Loading
  • Author Maintainer

    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 and Dirichlet both derive from Assign, and F0PsPhiInf derives from F0Ps. The constructor of Assign is Assign(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 to SWIG_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 as freestream = Freestream(msh, "group", Fct1C(...)), where Fct1C derives from Fct1.

    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:

    1. What is happening and why?
    2. Why do the classes deriving from flow::F0Ps cause an issue, while the classes deriving from tbox::Fct0 do not?
    3. 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:

    1. This issue https://github.com/swig/swig/issues/731 is still opened in the SWIG repo and is basically the same I run through
    2. If I change Assign to accept the derived class F0PsPhiInf (instead of the base F0Ps), it works fine. I also tried to replace F0ps by Fct0, and it runs.
    3. I encountered no issue, nor memory leaks due to the combined use of shared_ptr and director, which was my initial fear.

    Many thanks in advance.

  • Author Maintainer

    On ubuntu 18.04, all tests are now running whether in Release or Debug.

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment