Fixed memory leaks on the C++ side.