fixed error with memory allocation and release.