Fix some double-frees/use-after-free

r? @frank-trampe