Correctly tidy up after a failure to attach.
Correctly set error to non-zero upon an error.

Otherwise we'd end up calling *detach() during detach, double-freeing
things and causing a panic.