fix segfault after leaving scan in some conditions