#Issue 4: Refactor c++ code which cause memory leaks