<div dir="ltr">Thanks a lot, Constantine. It works pretty well.<div><br></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jun 16, 2023 at 6:52 PM Constantine Khrulev <<a href="mailto:ckhroulev@alaska.edu">ckhroulev@alaska.edu</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">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>
</blockquote></div>