Index: src/Makefile.am
===================================================================
--- src/Makefile.am	(revision 29812d568aaa49fa5ca9e12066f70b95b8cfe954)
+++ src/Makefile.am	(revision c30180691174b61d9244d1942760db46f34f09e0)
@@ -1,6 +1,6 @@
 SOURCE = atom.cpp bond.cpp boundary.cpp config.cpp element.cpp ellipsoid.cpp helpers.cpp leastsquaremin.cpp linkedcell.cpp moleculelist.cpp molecules.cpp parser.cpp periodentafel.cpp tesselation.cpp tesselationhelpers.cpp vector.cpp verbose.cpp
-HEADER = atom.hpp bond.hpp boundary.hpp config.hpp defs.hpp element.hpp ellipsoid.hpp helpers.hpp leastsquaremin.hpp linkedcell.hpp molecules.hpp parser.hpp periodentafel.hpp stackclass.hpp tesselation.hpp tesselationhelpers.hpp vector.hpp verbose.hpp
+HEADER = atom.hpp bond.hpp boundary.hpp config.hpp defs.hpp element.hpp ellipsoid.hpp helpers.hpp leastsquaremin.hpp linkedcell.hpp memoryallocator.hpp memoryusageobserver.cpp memoryusageobserver.hpp molecules.hpp parser.hpp periodentafel.hpp stackclass.hpp tesselation.hpp tesselationhelpers.hpp vector.hpp verbose.hpp
 
-noinst_PROGRAMS =  VectorUnitTest MemoryAllocatorUnitTest
+noinst_PROGRAMS =  VectorUnitTest MemoryAllocatorUnitTest MemoryUsageObserverUnitTest
 
 bin_PROGRAMS = molecuilder joiner analyzer
@@ -8,17 +8,20 @@
 molecuilder_DATA = elements.db valence.db orbitals.db Hbonddistance.db Hbondangle.db
 molecuilder_SOURCES =  ${SOURCE} builder.cpp  ${HEADER}
-joiner_SOURCES = joiner.cpp datacreator.cpp element.cpp helpers.cpp periodentafel.cpp parser.cpp verbose.cpp datacreator.hpp helpers.hpp parser.hpp periodentafel.hpp 
-analyzer_SOURCES = analyzer.cpp datacreator.cpp element.cpp helpers.cpp periodentafel.cpp parser.cpp verbose.cpp helpers.hpp periodentafel.hpp parser.hpp datacreator.hpp 
+joiner_SOURCES = joiner.cpp datacreator.cpp element.cpp helpers.cpp memoryallocator.hpp memoryusageobserver.cpp memoryusageobserver.hpp periodentafel.cpp parser.cpp verbose.cpp datacreator.hpp helpers.hpp parser.hpp periodentafel.hpp 
+analyzer_SOURCES = analyzer.cpp datacreator.cpp element.cpp helpers.cpp memoryallocator.hpp memoryusageobserver.cpp memoryusageobserver.hpp periodentafel.cpp parser.cpp verbose.cpp helpers.hpp periodentafel.hpp parser.hpp datacreator.hpp 
 
 TESTS = VectorUnitTest MemoryAllocatorUnitTest
 check_PROGRAMS = $(TESTS)
-VectorUnitTest_SOURCES = defs.hpp helpers.cpp helpers.hpp leastsquaremin.cpp leastsquaremin.hpp vectorunittest.cpp vectorunittest.hpp vector.cpp vector.hpp verbose.cpp verbose.hpp 
+VectorUnitTest_SOURCES = defs.hpp helpers.cpp helpers.hpp leastsquaremin.cpp leastsquaremin.hpp memoryallocator.hpp memoryusageobserver.cpp memoryusageobserver.hpp vectorunittest.cpp vectorunittest.hpp vector.cpp vector.hpp verbose.cpp verbose.hpp 
 VectorUnitTest_CXXFLAGS = $(CPPUNIT_CFLAGS)
 VectorUnitTest_LDFLAGS = $(CPPUNIT_LIBS) -ldl
 
-MemoryAllocatorUnitTest_SOURCES = defs.hpp helpers.cpp helpers.hpp memoryallocatorunittest.cpp memoryallocatorunittest.hpp memoryallocator.hpp verbose.cpp verbose.hpp 
+MemoryAllocatorUnitTest_SOURCES = defs.hpp helpers.cpp helpers.hpp memoryallocatorunittest.cpp memoryallocatorunittest.hpp memoryallocator.hpp memoryusageobserver.cpp memoryusageobserver.hpp verbose.cpp verbose.hpp 
 MemoryAllocatorUnitTest_CXXFLAGS = $(CPPUNIT_CFLAGS)
 MemoryAllocatorUnitTest_LDFLAGS = $(CPPUNIT_LIBS) -ldl
 
+MemoryUsageObserverUnitTest_SOURCES = defs.hpp helpers.cpp helpers.hpp memoryusageobserverunittest.cpp memoryusageobserverunittest.hpp memoryusageobserver.hpp memoryusageobserver.cpp verbose.cpp verbose.hpp 
+MemoryUsageObserverUnitTest_CXXFLAGS = $(CPPUNIT_CFLAGS)
+MemoryUsageObserverUnitTest_LDFLAGS = $(CPPUNIT_LIBS) -ldl
 
 EXTRA_DIST = ${molecuilder_DATA}
Index: src/builder.cpp
===================================================================
--- src/builder.cpp	(revision 29812d568aaa49fa5ca9e12066f70b95b8cfe954)
+++ src/builder.cpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
@@ -53,4 +53,5 @@
 #include "ellipsoid.hpp"
 #include "helpers.hpp"
+#include "memoryusageobserverunittest.hpp"
 #include "molecules.hpp"
 /********************************************* Subsubmenu routine ************************************/
@@ -1542,6 +1543,6 @@
                   delete(Subgraphs);
                   for (int i=0;i<FragmentCounter;i++)
-                    Free((void **)&ListOfLocalAtoms[FragmentCounter], "ParseCommandLineOptions: **ListOfLocalAtoms[]");
-                  Free((void **)&ListOfLocalAtoms, "ParseCommandLineOptions: ***ListOfLocalAtoms");
+                    Free(&ListOfLocalAtoms[FragmentCounter]);
+                  Free(&ListOfLocalAtoms);
                 }
                 delete(BackEdgeStack);
@@ -2021,4 +2022,6 @@
       delete(molecules); // also free's all molecules contained
       delete(periode);
+      cout << Verbose(0) <<  "Remaining non-freed memory: "
+        << MemoryUsageObserver::getInstance()->getUsedMemorySize() << endl;
       return 0;
       break;
@@ -2119,4 +2122,8 @@
   delete(molecules); // also free's all molecules contained
   delete(periode);
+
+  cout << Verbose(0) <<  "Remaining non-freed memory: "
+    << MemoryUsageObserver::getInstance()->getUsedMemorySize() << endl;
+
   return (0);
 }
Index: src/helpers.cpp
===================================================================
--- src/helpers.cpp	(revision 29812d568aaa49fa5ca9e12066f70b95b8cfe954)
+++ src/helpers.cpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
@@ -6,4 +6,5 @@
 
 #include "helpers.hpp"
+#include "memoryusageobserver.hpp"
 
 /********************************************** helpful functions *********************************/
@@ -133,7 +134,10 @@
   buffer[size] = '\0';
 
-  if (buffer == NULL)
+  if (buffer != NULL) {
+    MemoryUsageObserver::getInstance()->addMemory(buffer, size);
+  } else {
     cout << Verbose(0) << "Malloc for datatype " << typeid(char).name()
       << " failed - pointer is NULL: " << output << endl;
+  }
 
   return buffer;
Index: src/memoryallocator.hpp
===================================================================
--- src/memoryallocator.hpp	(revision 29812d568aaa49fa5ca9e12066f70b95b8cfe954)
+++ src/memoryallocator.hpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
@@ -24,4 +24,5 @@
 #include "defs.hpp"
 #include "verbose.hpp"
+#include "memoryusageobserver.hpp"
 
 /******************* wrappers for memory allocation functions ***********************/
@@ -40,7 +41,10 @@
   buffer = (X*) malloc(sizeof(X) * size);
 
-  if (buffer == NULL)
+  if (buffer != NULL) {
+    MemoryUsageObserver::getInstance()->addMemory(buffer, size);
+  } else {
     cout << Verbose(0) << "Malloc for datatype " << typeid(X).name()
       << " failed - pointer is NULL: " << output << endl;
+  }
 
   return buffer;
@@ -62,7 +66,11 @@
   X* buffer = NULL;
   buffer = (X*) calloc(sizeof(X) * size, (size_t) 0);
-  if (buffer == NULL)
+
+  if (buffer != NULL) {
+    MemoryUsageObserver::getInstance()->addMemory(buffer, size);
+  } else {
     cout << Verbose(0) << "Calloc for datatype " << typeid(X).name()
       << " failed - pointer is NULL: " << output << endl;
+  }
 
   return buffer;
@@ -82,11 +90,16 @@
 {
   X* buffer = NULL;
-  if (OldPointer == NULL)
+  if (OldPointer == NULL) {
     buffer = (X*) malloc(sizeof(X) * size);
-  else
+  } else {
     buffer = (X*) realloc(OldPointer, sizeof(X) * size);
-  if (buffer == NULL)
+    MemoryUsageObserver::getInstance()->removeMemory(OldPointer);
+  }
+  if (buffer != NULL) {
+    MemoryUsageObserver::getInstance()->addMemory(buffer, size);
+  } else {
     cout << Verbose(0) << "ReAlloc for datatype " << typeid(X).name()
       << " failed - new is NULL: " << output << endl;
+  }
 
   return buffer;
@@ -103,4 +116,5 @@
     return;
 
+  MemoryUsageObserver::getInstance()->removeMemory(*buffer);
   free(*buffer);
   *buffer = NULL;
Index: src/memoryusageobserver.cpp
===================================================================
--- src/memoryusageobserver.cpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
+++ src/memoryusageobserver.cpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
@@ -0,0 +1,90 @@
+/*
+ * \file memoryusageobserver.cpp
+ *
+ * This class represents a Singleton for observing memory usage.
+ */
+#include "memoryusageobserver.hpp"
+
+MemoryUsageObserver* MemoryUsageObserver::instance = NULL;
+
+/**
+ * Constructor. Do not use this function. Use getInstance() instead.
+ *
+ * \return memory usage observer instance
+ */
+MemoryUsageObserver::MemoryUsageObserver() {
+  instance = NULL;
+}
+
+/**
+ * Destructor. Better use purgeInstance().
+ */
+MemoryUsageObserver::~MemoryUsageObserver() {
+  for (map<void*, size_t>::iterator current = memoryUsers.begin(); current != memoryUsers.end(); current++) {
+    memoryUsers.erase(current);
+  }
+}
+
+/**
+ * Returns the singleton memory usage observer instance.
+ *
+ * \return memory usage observer instance
+ */
+MemoryUsageObserver* MemoryUsageObserver::getInstance() {
+  if (instance == NULL) {
+    instance = new MemoryUsageObserver;
+  }
+
+  return instance;
+}
+
+/**
+ * Purges the current memory usage observer instance.
+ */
+void MemoryUsageObserver::purgeInstance() {
+  if (instance != NULL) {
+    delete instance;
+  }
+
+  instance = NULL;
+}
+
+/**
+ * Adds memory.
+ *
+ * \param pointer to the allocated piece of memory
+ * \param size of the allocated memory
+ */
+void MemoryUsageObserver::addMemory(void* pointer, size_t size) {
+  memoryUsers[pointer] = size;
+}
+
+/**
+ * Removes tracked memory. Prints a warning if untracked memory is to be released.
+ *
+ * \param pointer to the allocated piece of memory
+ */
+void MemoryUsageObserver::removeMemory(void* pointer) {
+  map<void*, size_t>::iterator current = memoryUsers.find(pointer);
+
+  if (current == memoryUsers.end()) {
+    cout << "WARNING: There is non-tracked memory to be freed. Pointer "
+      << pointer << " is not registered by MemoryUsageObserver." << endl;
+    return;
+  }
+
+  memoryUsers.erase(current);
+}
+
+/**
+ * Gets the size of currently allocated memory.
+ */
+size_t MemoryUsageObserver::getUsedMemorySize() {
+  size_t totalSize = 0;
+
+  for (map<void*, size_t>::iterator current = memoryUsers.begin(); current != memoryUsers.end(); current++) {
+    totalSize += current->second;
+  }
+
+  return totalSize;
+}
Index: src/memoryusageobserver.hpp
===================================================================
--- src/memoryusageobserver.hpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
+++ src/memoryusageobserver.hpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
@@ -0,0 +1,43 @@
+/*
+ * \file memoryusageobserver.hpp
+ *
+ * This class represents a Singleton for observing memory usage.
+ */
+#ifndef MEMORYUSAGEOBSERVER_HPP_
+#define MEMORYUSAGEOBSERVER_HPP_
+
+using namespace std;
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <map>
+#include <iostream>
+#include <iomanip>
+#include <fstream>
+#include <sstream>
+#include <math.h>
+#include <string>
+#include <typeinfo>
+
+class MemoryUsageObserver {
+public:
+  static MemoryUsageObserver* getInstance();
+  static void purgeInstance();
+  void addMemory(void* pointer, size_t size);
+  void removeMemory(void* pointer);
+  size_t getUsedMemorySize();
+
+protected:
+  /** Do not call this constructor directly, use getInstance() instead. */
+  MemoryUsageObserver();
+  /** Do not call this destructor directly, use purgeInstance() instead. */
+  ~MemoryUsageObserver();
+
+private:
+  static MemoryUsageObserver* instance;
+  map<void*, size_t> memoryUsers;
+};
+#endif /*MEMORYUSAGEOBSERVER_HPP_*/
Index: src/memoryusageobserverunittest.cpp
===================================================================
--- src/memoryusageobserverunittest.cpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
+++ src/memoryusageobserverunittest.cpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
@@ -0,0 +1,125 @@
+/*
+ * memoryusageobserverunittest.cpp
+ */
+
+
+using namespace std;
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+
+#include "memoryusageobserver.hpp"
+#include "memoryusageobserverunittest.hpp"
+
+/********************************************** Test classes **************************************/
+// Registers the fixture into the 'registry'
+CPPUNIT_TEST_SUITE_REGISTRATION( MemoryUsageObserverTest );
+
+
+void MemoryUsageObserverTest::setUp()
+{
+};
+
+
+void MemoryUsageObserverTest::tearDown()
+{
+  MemoryUsageObserver::purgeInstance();
+};
+
+/**
+ * UnitTest for getInstance()
+ */
+void MemoryUsageObserverTest::getInstanceReturnsMemoryUsageObserverTest()
+{
+  CPPUNIT_ASSERT_EQUAL(true, MemoryUsageObserver::getInstance() != NULL);
+};
+
+/**
+ * UnitTest for getInstance()
+ */
+void MemoryUsageObserverTest::getInstanceTwiceReturnsSameInstanceTest()
+{
+  int* i = new int;
+  MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
+  CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
+  CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
+};
+
+/**
+ * UnitTest for getInstance()
+ */
+void MemoryUsageObserverTest::getInstanceAfterPurgeInstanceReturnsNewInstanceTest()
+{
+  int* i = new int;
+  MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
+  CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
+  MemoryUsageObserver::purgeInstance();
+  CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize());
+};
+
+/**
+ * UnitTest for addMemory() and removeMemory()
+ */
+void MemoryUsageObserverTest::addAndRemoveMemoryTest()
+{
+  int* i = new int;
+  MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
+  CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
+  MemoryUsageObserver::getInstance()->removeMemory(i);
+  CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize());
+};
+
+/**
+ * UnitTest for addMemory() and removeMemory()
+ */
+void MemoryUsageObserverTest::removeNonTrackedMemoryDoesNotCauseACrashTest()
+{
+  int* i = new int;
+  MemoryUsageObserver::getInstance()->removeMemory(i);
+  CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize());
+};
+
+/**
+ * UnitTest for addMemory() and removeMemory()
+ */
+void MemoryUsageObserverTest::addMemoryTwiceTest()
+{
+  int* i = new int;
+  int* j = new int;
+  MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
+  MemoryUsageObserver::getInstance()->addMemory(j, sizeof(int));
+  CPPUNIT_ASSERT_EQUAL(2 * sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
+ };
+
+/**
+ * UnitTest for addMemory() and removeMemory()
+ */
+void MemoryUsageObserverTest::addMemoryAndChangeSizeOfAddedMemoryTest()
+{
+  int* i = new int;
+  MemoryUsageObserver::getInstance()->addMemory(i, 2 * sizeof(int));
+  MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
+  CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
+ };
+
+/********************************************** Main routine **************************************/
+
+int main(int argc, char **argv)
+{
+  // Get the top level suite from the registry
+  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
+
+  // Adds the test to the list of test to run
+  CppUnit::TextUi::TestRunner runner;
+  runner.addTest( suite );
+
+  // Change the default outputter to a compiler error format outputter
+  runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
+                                                       std::cerr ) );
+  // Run the tests.
+  bool wasSucessful = runner.run();
+
+  // Return error code 1 if the one of test failed.
+  return wasSucessful ? 0 : 1;
+};
Index: src/memoryusageobserverunittest.hpp
===================================================================
--- src/memoryusageobserverunittest.hpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
+++ src/memoryusageobserverunittest.hpp	(revision c30180691174b61d9244d1942760db46f34f09e0)
@@ -0,0 +1,40 @@
+/*
+ * memoryusageobserverunittest.hpp
+ */
+
+#ifndef MEMORYUSAGEOBSERVERTEST_HPP_
+#define MEMORYUSAGEOBSERVERTEST_HPP_
+
+#include "memoryusageobserver.hpp"
+#include <cppunit/extensions/HelperMacros.h>
+
+
+/********************************************** Test classes **************************************/
+
+class MemoryUsageObserverTest : public CppUnit::TestFixture
+{
+    CPPUNIT_TEST_SUITE( MemoryUsageObserverTest) ;
+    CPPUNIT_TEST ( getInstanceReturnsMemoryUsageObserverTest );
+    CPPUNIT_TEST ( getInstanceTwiceReturnsSameInstanceTest );
+    CPPUNIT_TEST ( getInstanceAfterPurgeInstanceReturnsNewInstanceTest );
+    CPPUNIT_TEST ( addAndRemoveMemoryTest );
+    CPPUNIT_TEST ( removeNonTrackedMemoryDoesNotCauseACrashTest );
+    CPPUNIT_TEST ( addMemoryTwiceTest );
+    CPPUNIT_TEST ( addMemoryAndChangeSizeOfAddedMemoryTest );
+    CPPUNIT_TEST_SUITE_END();
+
+public:
+    void setUp();
+    void tearDown();
+
+    void getInstanceReturnsMemoryUsageObserverTest();
+    void getInstanceTwiceReturnsSameInstanceTest();
+    void getInstanceAfterPurgeInstanceReturnsNewInstanceTest();
+    void addAndRemoveMemoryTest();
+    void removeNonTrackedMemoryDoesNotCauseACrashTest();
+    void addMemoryTwiceTest();
+    void addMemoryAndChangeSizeOfAddedMemoryTest();
+};
+
+#endif /* MEMORYUSAGEOBSERVERTEST_HPP_ */
+
