Fix memory leaks (I've searched the similar problems in all formats after all.)