Code fixes

Various code fixes, mostly discovered by MSVC code static analyzer .
I'd throw an exception as alternative.
I don't think that printf/log write will work in case of out of memory.