encoder: fix memory leak on detach