<div dir="ltr">Great, thanks a lot.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Jun 21, 2023 at 5:12 PM Jacob Faibussowitsch <<a href="mailto:jacob.fai@gmail.com">jacob.fai@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">> If the line "DMDestroy(dmDist)" is commented out, the error will go away<br>
<br>
> > if (dmDist) {<br>
> > DMDestroy(&dm);<br>
> > dm = dmDist;<br>
> > }<br>
> > DMDestroy(&dmDist);<br>
<br>
<br>
This is because you are double-destroying dmDist here. Note that all petsc objects are pointers, so assignment may not do what you think it does. In this case, DM is a struct *_p_DM.<br>
<br>
So removing DMDestroy(dmDist) is correct.<br>
<br>
Best regards,<br>
<br>
Jacob Faibussowitsch<br>
(Jacob Fai - booss - oh - vitch)<br>
<br>
> On Jun 21, 2023, at 16:58, neil liu <<a href="mailto:liufield@gmail.com" target="_blank">liufield@gmail.com</a>> wrote:<br>
> <br>
> It works well for one processor; but when I tried two processors using mpiexec -n 2 ./ex1, <br>
> there is an error shown as belows. If the line "DMDestroy(dmDist)" is commented out, the error will go away. This is a little confusing for me. <br>
> <br>
> [1]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------<br>
> [1]PETSC ERROR: Corrupt argument: <a href="https://petsc.org/release/faq/#valgrind" rel="noreferrer" target="_blank">https://petsc.org/release/faq/#valgrind</a><br>
> [1]PETSC ERROR: Object already free: Parameter # 1<br>
> [1]PETSC ERROR: See <a href="https://petsc.org/release/faq/" rel="noreferrer" target="_blank">https://petsc.org/release/faq/</a> for trouble shooting.<br>
> [1]PETSC ERROR: Petsc Release Version 3.19.1, Apr 30, 2023 <br>
> [1]PETSC ERROR: ./ex1 on a arch-linux-c-debug named <a href="http://kirin.remcominc.com" rel="noreferrer" target="_blank">kirin.remcominc.com</a> by xiaodong.liu Wed Jun 21 16:54:46 2023<br>
> [1]PETSC ERROR: Configure options --with-cc=gcc --with-cxx=g++ --with-fc=gfortran --download-mpich --download-fblaslapack --download-ctetgen<br>
> [1]PETSC ERROR: #1 DMDestroy() at /home/xiaodong.liu/Documents/petsc-3.19.1/src/dm/interface/dm.c:639<br>
> [0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------<br>
> [0]PETSC ERROR: Corrupt argument: <a href="https://petsc.org/release/faq/#valgrind" rel="noreferrer" target="_blank">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/" rel="noreferrer" target="_blank">https://petsc.org/release/faq/</a> for trouble shooting.<br>
> [0]PETSC ERROR: Petsc Release Version 3.19.1, Apr 30, 2023 <br>
> [0]PETSC ERROR: ./ex1 on a arch-linux-c-debug named <a href="http://kirin.remcominc.com" rel="noreferrer" target="_blank">kirin.remcominc.com</a> by xiaodong.liu Wed Jun 21 16:54:46 2023<br>
> [0]PETSC ERROR: Configure options --with-cc=gcc --with-cxx=g++ --with-fc=gfortran --download-mpich --download-fblaslapack --download-ctetgen<br>
> [0]PETSC ERROR: #1 DMDestroy() at /home/xiaodong.liu/Documents/petsc-3.19.1/src/dm/interface/dm.c:639<br>
> <br>
> On Tue, Jun 20, 2023 at 12:36 PM neil liu <<a href="mailto:liufield@gmail.com" target="_blank">liufield@gmail.com</a>> wrote:<br>
> Thanks a lot, Constantine. It works pretty well.<br>
> <br>
> <br>
> <br>
> On Fri, Jun 16, 2023 at 6:52 PM Constantine Khrulev <<a href="mailto:ckhroulev@alaska.edu" target="_blank">ckhroulev@alaska.edu</a>> wrote:<br>
> In your code the destructor of DMManage is called at the end of scope, <br>
> i.e. after the PetscFinalize() call.<br>
> <br>
> You should be able to avoid this error by putting "DMManage objDMManage" <br>
> in a code block to limit its scope and ensure that it is destroyed <br>
> before PETSc is finalized:<br>
> <br>
> int main(int argc, char** argv) {<br>
> PetscFunctionBeginUser;<br>
> PetscCall(PetscInitialize(&argc, &argv, NULL, help));<br>
> <br>
> {<br>
> DMManage objDMManage;<br>
> } // objDMManage is destroyed here<br>
> <br>
> PetscFinalize();<br>
> return 0;<br>
> }<br>
> <br>
> On 6/16/23 14:13, neil liu wrote:<br>
> > Dear Petsc developers,<br>
> ><br>
> > I am trying to use Petsc with C++. And came across one issue.<br>
> > Class DMManage has been defined, one default constructor and <br>
> > destructor has been defined there.<br>
> > The code has a runtime error, "double free or corruption". Finally I <br>
> > found that, this is due to PetscFinalize. If I called explicitly the <br>
> > destructor before this PetscFinalze, the error will disappear.<br>
> ><br>
> > Does that mean PetscFinalize do some work to destroy DM?<br>
> ><br>
> > Thanks,<br>
> ><br>
> > #include <petscdmplex.h><br>
> > #include <petscdmadaptor.h><br>
> > #include <petscds.h><br>
> > #include <petscviewerhdf5.h><br>
> ><br>
> > class DMManage{<br>
> > PetscSF distributionSF;<br>
> > public:<br>
> > DM dm;<br>
> > DMManage();<br>
> > ~DMManage();<br>
> > };<br>
> ><br>
> > DMManage::DMManage(){<br>
> > const char filename[] = "ParallelWaveguide.msh";<br>
> > DM dmDist;<br>
> > PetscViewer viewer;<br>
> > PetscViewerCreate(PETSC_COMM_WORLD, &viewer);<br>
> > PetscViewerSetType(viewer, PETSCVIEWERASCII);<br>
> > PetscViewerFileSetMode(viewer, FILE_MODE_READ);<br>
> > PetscViewerFileSetName(viewer, filename);<br>
> > DMPlexCreateGmsh(PETSC_COMM_WORLD, viewer, PETSC_TRUE, &dm);<br>
> > PetscViewerDestroy(&viewer);<br>
> > PetscInt overlap = 0;<br>
> > DMPlexDistribute(dm, overlap, &distributionSF, &dmDist);<br>
> > std::cout<<&dm<<std::endl;<br>
> > if (dmDist) {<br>
> > DMDestroy(&dm);<br>
> > dm = dmDist;<br>
> > }<br>
> > DMDestroy(&dmDist);<br>
> > }<br>
> ><br>
> > DMManage::~DMManage(){<br>
> > DMDestroy(&dm);<br>
> > }<br>
> ><br>
> > int main(int argc, char** argv) {<br>
> > PetscFunctionBeginUser;<br>
> > PetscCall(PetscInitialize(&argc, &argv, NULL, help));<br>
> ><br>
> > DMManage objDMManage;<br>
> ><br>
> > PetscFinalize();<br>
> > return 0;<br>
> > }<br>
> <br>
> -- <br>
> Constantine<br>
> <br>
<br>
</blockquote></div>