[RFC] Fix memleaks

Comments welcome!

Ping @FernetMenta, @jmarshallnz
No: "This function can only be used if s was opened by avio_open()."