free allocated memory before error return