Index: /osgVisual/CMakeLists.txt
===================================================================
--- /osgVisual/CMakeLists.txt	(revision 85)
+++ /osgVisual/CMakeLists.txt	(revision 86)
@@ -263,4 +263,6 @@
 	src/core/visual_core.cpp
 	src/core/osgVisual.cpp
+	# Memory Leak debugging
+	include/core/leakDetection.h
 	# Util
 	include/util/visual_util.h
Index: /osgVisual/include/core/leakDetection.h
===================================================================
--- /osgVisual/include/core/leakDetection.h	(revision 86)
+++ /osgVisual/include/core/leakDetection.h	(revision 86)
@@ -0,0 +1,27 @@
+/* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer
+ *
+ * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under 
+ * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
+ * (at your option) any later version.  The full license is in LICENSE file
+ * included with this distribution, and on the openscenegraph.org website.
+ *
+ * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
+ * You have to aquire licenses for all used proprietary modules.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * OpenSceneGraph Public License for more details.
+*/
+
+// Basic ideas for memory leak debugging:
+// http://weseetips.com/2008/06/17/how-to-detect-memory-leaks-by-using-crt/
+// http://stackoverflow.com/questions/532917/dealing-with-incorrect-memory-leak-reports-on-windows
+// http://msdn.microsoft.com/en-us/library/x98tx3cf.aspx
+
+#ifdef _DEBUG
+	#ifndef DBG_NEW
+		#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
+		#define new DBG_NEW
+	#endif
+#endif
Index: /osgVisual/include/dataIO/visual_dataIO.h
===================================================================
--- /osgVisual/include/dataIO/visual_dataIO.h	(revision 85)
+++ /osgVisual/include/dataIO/visual_dataIO.h	(revision 86)
@@ -16,6 +16,6 @@
 */
 
-#include <osg/notify>
-#include <osg/referenced>
+#include <osg/Notify>
+#include <osg/Referenced>
 #include <osg/ArgumentParser>
 
Index: /osgVisual/src/core/osgVisual.cpp
===================================================================
--- /osgVisual/src/core/osgVisual.cpp	(revision 85)
+++ /osgVisual/src/core/osgVisual.cpp	(revision 86)
@@ -15,4 +15,9 @@
 */
 
+// Declare this in header.
+//#include <windows.h>
+#define _CRTDBG_MAP_ALLOC
+#include <crtdbg.h>
+
 #include <osg/ArgumentParser>
 #include <osg/Referenced>
@@ -21,10 +26,37 @@
 #include <visual_core.h>
 
+const unsigned int MAX_NUM_EVENTS = 10;
 
-const unsigned int MAX_NUM_EVENTS = 10;
-const unsigned int SWAP_BYTES_COMPARE = 0x12345678;
 
 int main(int argc, char** argv)
 {
+	
+#ifdef _DEBUG
+	#include <leakDetection.h>	// ugly but must be included inside the class
+
+    int tmp_flag;
+
+    HANDLE log_file = CreateFile("mem_log.txt", GENERIC_WRITE,FILE_SHARE_WRITE,
+        NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+
+    _CrtSetReportMode(_CRT_ASSERT,_CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW |
+        _CRTDBG_MODE_DEBUG);
+    _CrtSetReportMode(_CRT_WARN,_CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
+    _CrtSetReportMode(_CRT_ERROR,_CRTDBG_MODE_FILE | _CRTDBG_MODE_WNDW |
+        _CRTDBG_MODE_DEBUG);
+
+    // output to the file if not under VS
+    _CrtSetReportFile(_CRT_ASSERT, log_file);
+    _CrtSetReportFile(_CRT_WARN, log_file);
+    _CrtSetReportFile(_CRT_ERROR, log_file);
+
+    tmp_flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
+    tmp_flag |= _CRTDBG_ALLOC_MEM_DF;
+    tmp_flag |= _CRTDBG_DELAY_FREE_MEM_DF;
+    tmp_flag |= _CRTDBG_LEAK_CHECK_DF;
+
+    _CrtSetDbgFlag(tmp_flag);
+#endif 
+
     // Use an ArgumentParser object to manage the program arguments.
 	osg::ArgumentParser arguments(&argc,argv);
@@ -36,4 +68,7 @@
     core->initialize();
 
+	// Set Pointer to null to destroy the objects before this function ends - otherwise memory leaks wil be detected.
+	core = NULL;
+
 	return 0;
 }
Index: /osgVisual/src/core/visual_core.cpp
===================================================================
--- /osgVisual/src/core/visual_core.cpp	(revision 85)
+++ /osgVisual/src/core/visual_core.cpp	(revision 86)
@@ -15,4 +15,5 @@
 */
 
+
 #include <visual_core.h>
 
@@ -22,4 +23,22 @@
 {
 	OSG_NOTIFY( osg::ALWAYS ) << "visual_core instantiated." << std::endl;
+	#include <leakDetection.h>
+}
+
+visual_core::~visual_core(void)
+{
+	// shut osgVisual down
+	shutdown();
+
+	OSG_NOTIFY( osg::ALWAYS ) << "visual_core destroyed." << std::endl;
+
+}
+
+void visual_core::initialize()
+{
+	OSG_NOTIFY( osg::ALWAYS ) << "Initialize visual_core..." << std::endl;
+
+	// Configure osg to use KdTrees
+	osgDB::Registry::instance()->setBuildKdTreesHint(osgDB::ReaderWriter::Options::BUILD_KDTREES);
 
 	// Setup pathes
@@ -34,5 +53,5 @@
 
 	// Test memory leak (todo)
-	//double* test = new double[1000];
+	double* test = new double[1000];
 
 	#ifdef USE_SPACENAVIGATOR
@@ -41,21 +60,4 @@
 
 	//osg::DisplaySettings::instance()->setNumOfDatabaseThreadsHint( 8 );
-}
-
-visual_core::~visual_core(void)
-{
-	// shut osgVisual down
-	shutdown();
-
-	OSG_NOTIFY( osg::ALWAYS ) << "visual_core destroyed." << std::endl;
-
-}
-
-void visual_core::initialize()
-{
-	OSG_NOTIFY( osg::ALWAYS ) << "Initialize visual_core..." << std::endl;
-
-	// Load terrain
-	//loadTerrain(arguments);
 
 	// Show model
@@ -73,5 +75,4 @@
 #ifdef USE_SKY_SILVERLINING
 	// Initialize sky
-	OSG_NOTIFY( osg::ALWAYS ) << "Using Sky without distortion." << std::endl;
 	sky = new visual_skySilverLining( viewer );
 	sky->init(distortedSceneGraph, rootNode);	// Without distortedSceneGraph=NULL
@@ -156,4 +157,7 @@
 	}
 #endif
+
+	// Destroy osgViewer
+	viewer = NULL;
 }
 
