Remove memory leaks by introducing shared_ptr into code.