Fix memory leak if string is incorrectly formatted