cpp codes, memory leak fix