memcpy: free buffer in case of failure