There are many places in code where it is assumed that allocated memory or variables are filled with 0's, that is hardly the case, especially with malloc()'ated memory.
Also there are few places where mallocated memory is deleted, not freed, this should fix'em.
I strongly recommend running the program against valgrind, it's trivial to catch bugs like these with it.
fix for uninitalised memory and free()/delete mismatches