Finally fixed memory leak issues with ncurses.