[petsc-users] Still reachable memory in valgrind

Lawrence Mitchell wence at gmx.li
Tue Oct 12 09:01:37 CDT 2021


Hi Pierre,

> On 12 Oct 2021, at 14:58, Pierre Seize <pierre.seize at onera.fr> wrote:
> 
> 
>  1 #include <petscsys.h>
>  2
>  3 int main(int argc, char **argv){
>  4   PetscErrorCode ierr = 0;
>  5
>  6   ierr = PetscInitialize(&argc, &argv, NULL, ""); if (ierr) return ierr;
>  7   PetscReal *foo;
>  8   malloc(sizeof(PetscReal));
>  9   ierr = PetscMalloc1(1, &foo); CHKERRQ(ierr);

You need to call PetscFree on any arrays that you allocate with PetscMalloc, otherwise this is indeed a memory leak.

Similarly, you should call free on any pointers that you get from malloc to deallocate them.

Thanks,

Lawrence

> 10   ierr = PetscFinalize();
> 11   return ierr;
> 12 }



More information about the petsc-users mailing list