Could you add a pgd_close that does this? I think matching malloc and free (or new/delete) should generally be in the same .c file, for clarity.