check for memory allocation error in copy_image