fix realloc invalid pointer error