Correcting a bug freeing memory