core: fix memory leak when stopping main loop