Fixed mismached memory allocation and deallocation