fixed a bug that cause memory violation upon termination