[bug] Fix possible memory leaks

Fix wrong usage of realloc().
Found by VC's /analyze option.