Use c++ new[] instead of malloc().
Fixed some memory leaks.