Fix potential memory leak when unmounting

Fixes #781.