[RFC] Fix memleaks

Comments welcome!

Ping @FernetMenta, @jmarshallnz
avio_closep?