[petsc-users] Inquiry about the c++ destructor and PetscFinalize.

neil liu liufield at gmail.com
Wed Jun 21 16:27:23 CDT 2023


Great, thanks a lot.

On Wed, Jun 21, 2023 at 5:12 PM Jacob Faibussowitsch <jacob.fai at gmail.com>
wrote:

> > If the line "DMDestroy(dmDist)" is commented out, the error will go away
>
> > >   if (dmDist) {
> > >     DMDestroy(&dm);
> > >     dm = dmDist;
> > >   }
> > >   DMDestroy(&dmDist);
>
>
> 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.
>
> So removing DMDestroy(dmDist) is correct.
>
> Best regards,
>
> Jacob Faibussowitsch
> (Jacob Fai - booss - oh - vitch)
>
> > On Jun 21, 2023, at 16:58, neil liu <liufield at gmail.com> wrote:
> >
> > It works well for one processor; but when I tried two processors using
> mpiexec -n 2 ./ex1,
> > 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.
> >
> > [1]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> > [1]PETSC ERROR: Corrupt argument:
> https://petsc.org/release/faq/#valgrind
> > [1]PETSC ERROR: Object already free: Parameter # 1
> > [1]PETSC ERROR: See https://petsc.org/release/faq/ for trouble shooting.
> > [1]PETSC ERROR: Petsc Release Version 3.19.1, Apr 30, 2023
> > [1]PETSC ERROR: ./ex1 on a arch-linux-c-debug named kirin.remcominc.com
> by xiaodong.liu Wed Jun 21 16:54:46 2023
> > [1]PETSC ERROR: Configure options --with-cc=gcc --with-cxx=g++
> --with-fc=gfortran --download-mpich --download-fblaslapack
> --download-ctetgen
> > [1]PETSC ERROR: #1 DMDestroy() at
> /home/xiaodong.liu/Documents/petsc-3.19.1/src/dm/interface/dm.c:639
> > [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> > [0]PETSC ERROR: Corrupt argument:
> https://petsc.org/release/faq/#valgrind
> > [0]PETSC ERROR: Object already free: Parameter # 1
> > [0]PETSC ERROR: See https://petsc.org/release/faq/ for trouble shooting.
> > [0]PETSC ERROR: Petsc Release Version 3.19.1, Apr 30, 2023
> > [0]PETSC ERROR: ./ex1 on a arch-linux-c-debug named kirin.remcominc.com
> by xiaodong.liu Wed Jun 21 16:54:46 2023
> > [0]PETSC ERROR: Configure options --with-cc=gcc --with-cxx=g++
> --with-fc=gfortran --download-mpich --download-fblaslapack
> --download-ctetgen
> > [0]PETSC ERROR: #1 DMDestroy() at
> /home/xiaodong.liu/Documents/petsc-3.19.1/src/dm/interface/dm.c:639
> >
> > On Tue, Jun 20, 2023 at 12:36 PM neil liu <liufield at gmail.com> wrote:
> > Thanks a lot, Constantine. It works pretty well.
> >
> >
> >
> > On Fri, Jun 16, 2023 at 6:52 PM Constantine Khrulev <
> ckhroulev at alaska.edu> wrote:
> > In your code the destructor of DMManage is called at the end of scope,
> > i.e. after the PetscFinalize() call.
> >
> > You should be able to avoid this error by putting "DMManage objDMManage"
> > in a code block to limit its scope and ensure that it is destroyed
> > before PETSc is finalized:
> >
> > int main(int argc, char** argv) {
> >    PetscFunctionBeginUser;
> >    PetscCall(PetscInitialize(&argc, &argv, NULL, help));
> >
> >    {
> >      DMManage objDMManage;
> >     } // objDMManage is destroyed here
> >
> >    PetscFinalize();
> >    return 0;
> > }
> >
> > On 6/16/23 14:13, neil liu wrote:
> > > Dear Petsc developers,
> > >
> > > I am trying to use Petsc with C++. And came across one issue.
> > > Class DMManage has been defined, one default constructor and
> > > destructor has been defined there.
> > > The code has a runtime error, "double free or corruption". Finally I
> > > found that, this is due to PetscFinalize. If I called explicitly the
> > > destructor before this PetscFinalze, the error will disappear.
> > >
> > > Does that mean PetscFinalize do some work to destroy DM?
> > >
> > > Thanks,
> > >
> > > #include <petscdmplex.h>
> > > #include <petscdmadaptor.h>
> > > #include <petscds.h>
> > > #include <petscviewerhdf5.h>
> > >
> > > class DMManage{
> > >   PetscSF distributionSF;
> > > public:
> > >   DM dm;
> > >   DMManage();
> > >   ~DMManage();
> > > };
> > >
> > > DMManage::DMManage(){
> > >   const char filename[] = "ParallelWaveguide.msh";
> > >   DM dmDist;
> > >   PetscViewer viewer;
> > >   PetscViewerCreate(PETSC_COMM_WORLD, &viewer);
> > >   PetscViewerSetType(viewer, PETSCVIEWERASCII);
> > >   PetscViewerFileSetMode(viewer, FILE_MODE_READ);
> > >   PetscViewerFileSetName(viewer, filename);
> > >   DMPlexCreateGmsh(PETSC_COMM_WORLD, viewer, PETSC_TRUE, &dm);
> > >   PetscViewerDestroy(&viewer);
> > >   PetscInt overlap = 0;
> > >   DMPlexDistribute(dm, overlap, &distributionSF, &dmDist);
> > >   std::cout<<&dm<<std::endl;
> > >   if (dmDist) {
> > >     DMDestroy(&dm);
> > >     dm = dmDist;
> > >   }
> > >   DMDestroy(&dmDist);
> > > }
> > >
> > > DMManage::~DMManage(){
> > >   DMDestroy(&dm);
> > > }
> > >
> > > int main(int argc, char** argv) {
> > >   PetscFunctionBeginUser;
> > >   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
> > >
> > >   DMManage objDMManage;
> > >
> > >   PetscFinalize();
> > >   return 0;
> > > }
> >
> > --
> > Constantine
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20230621/a82f40a6/attachment-0001.html>


More information about the petsc-users mailing list