fix segfault/corruption on shutdown