Setting/releasing images on buttons

I have a program which requests pointers to PhImage_t structs for use on PtButtons, from a caching system I have written, the end game being that for many PtButtons, they get a pointer to the same image.

This works fine until I PtDestroyWidget the PtButtons, the first one to get destroyed is fine, but I get a segfault on destroying the next PtButton.

I’m guessing that the first PtDestroyWidget is freeing up some memory associated with the image, and the next destroy tries to free memory which is already freed.

I’ve tried setting the image resource in the PtButton to NULL before destroying, but this does not work, as I guess it’s doing exactly the same thing.

So, what can I do to let a PtButton use a PhImage_t but, leave it well alone on being destroyed?