winio: fix heap corruption and various leaks.
This fixes the heap corruption noticed in !6390 and various other kernel resource leaks.
Heap corruption was caused by an incorrect calling convention and an off by 1 error during malloc.
Part of enabling #20255.
Fixes #21048 (closed).
Also needs backporting to all open 9.x branches.