Fix some double-frees/use-after-free

r? @frank-trampe
Agreed, this falls under "some other code styles in use in this repo" ;)