[RFC] Fix memleaks

Comments welcome!

Ping @FernetMenta, @jmarshallnz