Fix CID 1402307: use after free, and do a proper freeing of struct.