Fix memory errors. Add sample CMakeLists.txt