Fix a possible double-free or segmentation fault.