Fixed leaks in dynamic array