Hello Juan,
Thank you for the code review! A quick clarification about HeapFree:
Some judicious use of goto could reduce code for all your error paths. Remember that HeapFree of a NULL pointer is allowed.
The docs for HeapFree state "If this pointer is NULL, the behavior is undefined." So in practice the undefined behavior is benign?
Peace, -Roy