Free ptr only if necessary (around CID 1491058).