Fix crash at exit with a moving surface. Fix #398.