PetscHeaderDestroy — PETSc 3.21.4 documentation

Skip to main content
`Ctrl`+`K`

# PetscHeaderDestroy

Final step in destroying a `PetscObject`

## Synopsis

    #include <petsc/private/petscimpl.h>
    PetscErrorCode PetscHeaderDestroy(PetscObject *obj)

## Input Parameter

- h - A pointer to the header created with `PetscHeaderCreate()`

## Notes

`h` is freed and set to `PETSC_NULLPTR` when this routine returns.

## Example Usage

      PetscObject obj;

      PetscHeaderCreate(obj, ...);
      // use obj...

      // note pointer to obj is used
      PetscHeaderDestroy(&obj);

Note that this routine is the last step when destroying higher-level `PetscObject`s as it deallocates the memory for the structure itself:

      typedef struct MyPetscObject_s *MyPetscObject;
      struct MyPetscObject_s
      {
        _p_PetscObject  hdr;
        PetscInt       *foo;
        PetscScalar    *bar;
      };

      // assume obj is created/initialized elsewhere...
      MyPetscObject obj;

      // OK, should dispose of all dynamically allocated resources before calling
      // PetscHeaderDestroy()
      PetscFree(obj->foo);

      // OK, dispose of obj
      PetscHeaderDestroy(&obj);

      // ERROR, obj points to NULL here, accessing obj->bar may result in segmentation violation!
      // obj->bar is potentially leaked!
      PetscFree(obj->bar);

## See Also

`PetscObject`, `PetscHeaderCreate()`

## Level

developer

## Location

include/petsc/private/petscimpl.h

## Examples

src/snes/tutorials/ex48.c
src/ts/tutorials/ex14.c

Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages