Fix crashes in the detach path. Submitted by: Kristof Provost <kristof@sigsegv.be> MFC after: 1 month