Fixed segfault on exit due to destruction order