Clean up memory when returning with an error.
Clean up memory when returning with an error.

Related to #584.

HIPERFIT/futhark,HIPERFIT/futhark,diku-dk/futhark,diku-dk/futhark,diku-dk/futhark,diku-dk/futhark,HIPERFIT/futhark,diku-dk/futhark