Fixing potential memory leaks.

This version fixes the potential memory leak in the ObjectFactory and adds new tests to the Test/CMakeLists.txt in order to check the memory leaks in Debug mode.