Print error before freeing memory