epub backend: fix a lot of memleaks

I didn't squash the commits because there's a lot of changes and I think it's easier to test them one by one.
*Almost* every commit is dealing with a single function.