[COVERITY] Fix out of bounds read.
Fix some warnings, fix a memory leak, some cosmetics.
[COVERITY] Fix leaks.
[COVERITY] Fix logic error that lead to dead code.
[COVERITY] Fix some null pointer's reads.
Fix a warning.
[COVERITY] Fix a leak.
MSDN discourages the use of global memory functions, except for specific cases
(e.g., clipboard operations), so move to heap memory functions.
Cosmetic.