<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>In my destructor I have the following phrase:</p>
    <p><i>                 if(local_vec != PETSC_NULL) {</i><i><br>
      </i><i>                    PetscInt vecSize = 0;</i><i><br>
      </i><i>                    PetscErrorCode ierr;</i><i><br>
      </i><i>                    std::cerr << local_vec <<
        '\n';</i><i><br>
      </i><i>                    ierr = VecGetSize(local_vec,
        &vecSize);</i><i><br>
      </i><i>                    std::cerr << vecSize <<
        '\t' << ierr << '\n';</i><i><br>
      </i><i>                    ierr = VecDestroy(&local_vec);</i><i><br>
      </i><i>                    std::cerr << vecSize <<
        '\t' << ierr << '\n';</i><i><br>
      </i><i>                    local_vec = PETSC_NULL;</i><i><br>
      </i><i>                 }</i></p>
    <p>which should set <i>local_vec</i> to <i>PETSC_NULL</i> as soon
      as it is no longer in use.</p>
    <p>Regards,</p>
    <p>Roland<br>
    </p>
    <div class="moz-cite-prefix">Am 01.04.22 um 13:19 schrieb Matthew
      Knepley:<br>
    </div>
    <blockquote type="cite" cite="mid:CAMYG4Gk2acJaejzZv=h522+sTZwekb0MgWjcRYZgOQCvRwFN6w@mail.gmail.com">
      
      <div dir="ltr">
        <div dir="ltr">On Fri, Apr 1, 2022 at 6:50 AM Roland Richter
          <<a href="mailto:roland.richter@ntnu.no" moz-do-not-send="true" class="moz-txt-link-freetext">roland.richter@ntnu.no</a>>
          wrote:<br>
        </div>
        <div class="gmail_quote">
          <blockquote class="gmail_quote" style="margin:0px 0px 0px
            0.8ex;border-left:1px solid
            rgb(204,204,204);padding-left:1ex">
            <div>
              <p>I re-run my code with a debug version of PETSc,
                resulting in <br>
              </p>
              <p><span style="font-family:monospace"><span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">[0]PETSC
                    ERROR: --------------------- Error Message
                    --------------------------------------------------------------</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> </span><br>
                  [0]PETSC ERROR: Corrupt argument: <a href="https://petsc.org/release/faq/#valgrind" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">https://petsc.org/release/faq/#valgrind</a>
                  <br>
                  [0]PETSC ERROR: Object already free: Parameter # 1 <br>
                  [0]PETSC ERROR: See <a href="https://petsc.org/release/faq/" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">https://petsc.org/release/faq/</a>
                  for trouble shooting. <br>
                  [0]PETSC ERROR: Petsc Development GIT revision:
                  v3.17.0-8-gf6d6129e50  GIT Date: 2022-03-31 18:10:33
                  +0000 <br>
                  [0]PETSC ERROR: #1 VecGetSize() at
/home/roland/Downloads/git-files/petsc/src/vec/vec/interface/vector.c:670<br>
                  0       64 <br>
                  [0]PETSC ERROR: #2 VecDestroy() at
/home/roland/Downloads/git-files/petsc/src/vec/vec/interface/vector.c:375<br>
                  0       64 <br>
                  [0]PETSC ERROR: #3 VecGetArray() at
/home/roland/Downloads/git-files/petsc/src/vec/vec/interface/rvector.c:1780<br>
                  <br>
                </span></p>
              <p>I do not understand why it tries to access the vector,
                even though it has been set to PETSC_NULL in the
                previous free-call.</p>
            </div>
          </blockquote>
          <div>What code is setting that pointer to NULL?</div>
          <div><br>
          </div>
          <div>  Thanks,</div>
          <div><br>
          </div>
          <div>    Matt </div>
          <blockquote class="gmail_quote" style="margin:0px 0px 0px
            0.8ex;border-left:1px solid
            rgb(204,204,204);padding-left:1ex">
            <div>
              <p>Regards,</p>
              <p>Roland<br>
              </p>
              <div>Am 31.03.22 um 15:50 schrieb Matthew Knepley:<br>
              </div>
              <blockquote type="cite">
                <div dir="ltr">
                  <div dir="ltr">On Thu, Mar 31, 2022 at 9:47 AM Roland
                    Richter <<a href="mailto:roland.richter@ntnu.no" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">roland.richter@ntnu.no</a>>
                    wrote:<br>
                  </div>
                  <div class="gmail_quote">
                    <blockquote class="gmail_quote" style="margin:0px
                      0px 0px 0.8ex;border-left:1px solid
                      rgb(204,204,204);padding-left:1ex">
                      <div>
                        <p>The backtrace is</p>
                        <p><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">#0
                               </span><span style="color:rgb(24,24,178);background-color:rgb(255,255,255)">0x00007fffeec4ba97</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> in </span><span style="color:rgb(178,104,24);background-color:rgb(255,255,255)">VecGetSize_Seq</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> () from </span><span style="color:rgb(24,178,24);background-color:rgb(255,255,255)">/opt/petsc/lib/libpetsc.so.3.016</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> </span><br>
                            #1  <span style="color:rgb(24,24,178);background-color:rgb(255,255,255)">0x00007fffeec78f5a</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> in </span><span style="color:rgb(178,104,24);background-color:rgb(255,255,255)">VecGetSize</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> () from </span><span style="color:rgb(24,178,24);background-color:rgb(255,255,255)">/opt/petsc/lib/libpetsc.so.3.016</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> </span><br>
                            #2  <span style="color:rgb(24,24,178);background-color:rgb(255,255,255)">0x0000000000410b73</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> in </span><span style="color:rgb(178,104,24);background-color:rgb(255,255,255)">test_ts_arma_with_pure_petsc_preconfigured_clean(unsigned
                              long, unsigned long,
                              arma::Col<std::complex<doubl</span><br>
                            e> > const&,
                            arma::Col<std::complex<double>
                            >&, double, double, double) [clone
                            .constprop.0]<span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">
                              () </span><br>
                            #3  <span style="color:rgb(24,24,178);background-color:rgb(255,255,255)">0x0000000000414384</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> in </span><span style="color:rgb(178,104,24);background-color:rgb(255,255,255)">test_RK4_solvers_clean(unsigned
                              long, unsigned long, unsigned long, bool)
                              [clone .constprop.0]</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">
                              () </span><br>
                            #4  <span style="color:rgb(24,24,178);background-color:rgb(255,255,255)">0x0000000000405c6c</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> in </span><span style="color:rgb(178,104,24);background-color:rgb(255,255,255)">main</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> ()</span></span></p>
                      </div>
                    </blockquote>
                    <div>It looks like you are passing an invalid
                      vector. If you compiled in debug mode, it would
                      tell you. I would run</div>
                    <div>in debug until my code was running like I
                      expect, then switch to optimized. You can do that
                      by using two</div>
                    <div>different PETSC_ARCH configures, and switch at
                      runtime with that variable.</div>
                    <div><br>
                    </div>
                    <div>  Thanks,</div>
                    <div><br>
                    </div>
                    <div>    Matt </div>
                    <blockquote class="gmail_quote" style="margin:0px
                      0px 0px 0.8ex;border-left:1px solid
                      rgb(204,204,204);padding-left:1ex">
                      <div>
                        <p>Regards,</p>
                        <div>Roland Richter</div>
                        <div><br>
                        </div>
                        <div>Am 31.03.22 um 15:35 schrieb Matthew
                          Knepley:<br>
                        </div>
                        <blockquote type="cite">
                          <div dir="ltr">
                            <div dir="ltr">On Thu, Mar 31, 2022 at 9:01
                              AM Roland Richter <<a href="mailto:roland.richter@ntnu.no" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">roland.richter@ntnu.no</a>>
                              wrote:<br>
                            </div>
                            <div class="gmail_quote">
                              <blockquote class="gmail_quote" style="margin:0px 0px 0px
                                0.8ex;border-left:1px solid
                                rgb(204,204,204);padding-left:1ex">
                                <div>
                                  <p>Hei,</p>
                                  <p>Thanks for the idea! I added a
                                    simple std::cout for both
                                    constructor and destructor, and
                                    found out that my destructor is
                                    called multiple times, while the
                                    constructor is called only once.
                                    This could explain the error (double
                                    free), but I do not know why
                                    segfault is thrown even though I
                                    explicitly check if the vector has
                                    been used. Are there explanations
                                    for that?</p>
                                </div>
                              </blockquote>
                              <div>Run with -start_in_debugger and get
                                the stack trace when it faults. Right
                                now, I have no idea where it is
                                faulting.</div>
                              <div><br>
                              </div>
                              <div>  Thanks,</div>
                              <div><br>
                              </div>
                              <div>    Matt</div>
                              <div> <br>
                              </div>
                              <blockquote class="gmail_quote" style="margin:0px 0px 0px
                                0.8ex;border-left:1px solid
                                rgb(204,204,204);padding-left:1ex">
                                <div>
                                  <p>Regards,</p>
                                  <p>Roland Richter<br>
                                  </p>
                                  <div>Am 31.03.22 um 12:14 schrieb
                                    Matthew Knepley:<br>
                                  </div>
                                  <blockquote type="cite">
                                    <div dir="ltr">
                                      <div dir="ltr">On Thu, Mar 31,
                                        2022 at 5:58 AM Roland Richter
                                        <<a href="mailto:roland.richter@ntnu.no" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">roland.richter@ntnu.no</a>>
                                        wrote:<br>
                                      </div>
                                      <div class="gmail_quote">
                                        <blockquote class="gmail_quote" style="margin:0px 0px 0px
                                          0.8ex;border-left:1px solid
                                          rgb(204,204,204);padding-left:1ex">
                                          <div>
                                            <p>Hei,</p>
                                            <p>For a project I wanted to
                                              combine boost::odeint for
                                              timestepping and
                                              PETSc-based vectors and
                                              matrices for calculating
                                              the right hand side. As
                                              comparison for both timing
                                              and correctness I set up
                                              an armadillo-based right
                                              hand side (with the
                                              main-function being in <b>main.cpp</b>,
                                              and the test code in <b>test_timestepping_clean.cpp</b>)<br>
                                            </p>
                                            <p>In theory, the code works
                                              fine, but I have some
                                              issues with cleaning up
                                              afterwards in my struct <i>Petsc_RHS_state_clean</i>.
                                              My initial intention was
                                              to set up all involved
                                              matrices and vectors
                                              within the constructor,
                                              and free the memory in the
                                              destructor. To avoid
                                              freeing vectors I have not
                                              used I initially set them
                                              to <i>PETSC_NULL</i>, and
                                              check if this value has
                                              been changed before
                                              calling <i>VecDestroy().</i></p>
                                          </div>
                                        </blockquote>
                                        <div>You do not need to check.
                                          Destroy() functions already
                                          check for NULL.</div>
                                        <blockquote class="gmail_quote" style="margin:0px 0px 0px
                                          0.8ex;border-left:1px solid
                                          rgb(204,204,204);padding-left:1ex">
                                          <div>
                                            <p> However, when doing that
                                              I get the following error:</p>
                                            <p><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0]PETSC
                                                  ERROR:
                                                  ------------------------------------------------------------------------
                                                </span><br>
                                                [0]PETSC ERROR: Caught
                                                signal number 11 SEGV:
                                                Segmentation Violation,
                                                probably memory access
                                                out of range <br>
                                                [0]PETSC ERROR: Try
                                                option
                                                -start_in_debugger or
                                                -on_error_attach_debugger
                                                <br>
                                                [0]PETSC ERROR: or see <a href="https://petsc.org/release/faq/#valgrind" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">https://petsc.org/release/faq/#valgrind</a>
                                                <br>
                                                [0]PETSC ERROR: or try <a href="http://valgrind.org" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">http://valgrind.org</a>
                                                on GNU/linux and Apple
                                                Mac OS X to find memory
                                                corruption errors <br>
                                                [0]PETSC ERROR:
                                                configure using
                                                --with-debugging=yes,
                                                recompile, link, and run
                                                 <br>
                                                [0]PETSC ERROR: to get
                                                more information on the
                                                crash. <br>
                                                <span style="font-weight:bold;color:rgb(255,84,84);background-color:rgb(255,255,255)">[0]PETSC
                                                  ERROR:
                                                  ---------------------
                                                  Error Message
--------------------------------------------------------------</span></span></p>
                                            <p>If I comment out that
                                              code in
                                              ~Petsc_RHS_state_clean(),
                                              the program runs, but will
                                              use ~17 GByte of RAM
                                              during runtime. As the
                                              memory is not used
                                              immediately in full, but
                                              rather increases during
                                              running, I assume a memory
                                              leak somewhere. Where does
                                              it come from, and how can
                                              I avoid it?</p>
                                          </div>
                                        </blockquote>
                                        <div>It must be that your
                                          constructor is called multiple
                                          times without calling your
                                          destructor. I cannot
                                          understand this code in order</div>
                                        <div>to see where that happens,
                                          but you should just be able to
                                          run in the debugger and put a
                                          break point at the creation
                                          and<br>
                                        </div>
                                        <div>destruction calls.</div>
                                        <div><br>
                                        </div>
                                        <div>  Thanks,</div>
                                        <div><br>
                                        </div>
                                        <div>      Matt</div>
                                        <blockquote class="gmail_quote" style="margin:0px 0px 0px
                                          0.8ex;border-left:1px solid
                                          rgb(204,204,204);padding-left:1ex">
                                          <div>
                                            <p>Thanks!</p>
                                            <p>Regards,</p>
                                            <p>Roland Richter<br>
                                            </p>
                                            <p><span style="font-family:monospace"></span></p>
                                          </div>
                                        </blockquote>
                                      </div>
                                      <br clear="all">
                                      <div><br>
                                      </div>
                                      -- <br>
                                      <div dir="ltr">
                                        <div dir="ltr">
                                          <div>
                                            <div dir="ltr">
                                              <div>
                                                <div dir="ltr">
                                                  <div>What most
                                                    experimenters take
                                                    for granted before
                                                    they begin their
                                                    experiments is
                                                    infinitely more
                                                    interesting than any
                                                    results to which
                                                    their experiments
                                                    lead.<br>
                                                    -- Norbert Wiener</div>
                                                  <div><br>
                                                  </div>
                                                  <div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" moz-do-not-send="true">https://www.cse.buffalo.edu/~knepley/</a><br>
                                                  </div>
                                                </div>
                                              </div>
                                            </div>
                                          </div>
                                        </div>
                                      </div>
                                    </div>
                                  </blockquote>
                                </div>
                              </blockquote>
                            </div>
                            <br clear="all">
                            <div><br>
                            </div>
                            -- <br>
                            <div dir="ltr">
                              <div dir="ltr">
                                <div>
                                  <div dir="ltr">
                                    <div>
                                      <div dir="ltr">
                                        <div>What most experimenters
                                          take for granted before they
                                          begin their experiments is
                                          infinitely more interesting
                                          than any results to which
                                          their experiments lead.<br>
                                          -- Norbert Wiener</div>
                                        <div><br>
                                        </div>
                                        <div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" moz-do-not-send="true">https://www.cse.buffalo.edu/~knepley/</a><br>
                                        </div>
                                      </div>
                                    </div>
                                  </div>
                                </div>
                              </div>
                            </div>
                          </div>
                        </blockquote>
                      </div>
                    </blockquote>
                  </div>
                  <br clear="all">
                  <div><br>
                  </div>
                  -- <br>
                  <div dir="ltr">
                    <div dir="ltr">
                      <div>
                        <div dir="ltr">
                          <div>
                            <div dir="ltr">
                              <div>What most experimenters take for
                                granted before they begin their
                                experiments is infinitely more
                                interesting than any results to which
                                their experiments lead.<br>
                                -- Norbert Wiener</div>
                              <div><br>
                              </div>
                              <div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" moz-do-not-send="true">https://www.cse.buffalo.edu/~knepley/</a><br>
                              </div>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </blockquote>
            </div>
          </blockquote>
        </div>
        <br clear="all">
        <div><br>
        </div>
        -- <br>
        <div dir="ltr" class="gmail_signature">
          <div dir="ltr">
            <div>
              <div dir="ltr">
                <div>
                  <div dir="ltr">
                    <div>What most experimenters take for granted before
                      they begin their experiments is infinitely more
                      interesting than any results to which their
                      experiments lead.<br>
                      -- Norbert Wiener</div>
                    <div><br>
                    </div>
                    <div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank" moz-do-not-send="true">https://www.cse.buffalo.edu/~knepley/</a><br>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </blockquote>
  </body>
</html>