Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp	(revision 3017)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/FrameBufferObject.cpp	(revision 3021)
@@ -1,5 +1,17 @@
-#include <iostream>
 #include "FrameBufferObject.h"
 #include "glInterface.h"
+#include <iostream>
+
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
+
 
 using namespace std;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Geometry.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Geometry.cpp	(revision 2980)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Geometry.cpp	(revision 3021)
@@ -3,4 +3,15 @@
 #include "glInterface.h"
 #include "RenderState.h"
+
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 2964)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/BvhLoader.cpp	(revision 3021)
@@ -6,4 +6,15 @@
 #include "BvhLoader.h"
 #include "gzstream.h"
+
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
 
 
@@ -65,5 +76,5 @@
 	BvhNode *root = LoadNextNode(stream, NULL);
 
-#if 0
+#if 1
 
 	bvh->mRoot = root;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ResourceManager.cpp	(revision 3021)
@@ -13,4 +13,15 @@
 
 
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
+
+
 using namespace std;
 
@@ -129,4 +140,6 @@
 		mTextureTable[i] = tex;
 		mTextures.push_back(tex);
+
+		delete [] texname;
 	}
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp	(revision 3001)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SceneQuery.cpp	(revision 3021)
@@ -11,4 +11,15 @@
 
 
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
+
+
 using namespace std;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp	(revision 2944)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Polyhedron.cpp	(revision 3021)
@@ -3,4 +3,15 @@
 #include "Polygon3.h"
 #include "Plane3.h"
+
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 3020)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp	(revision 3021)
@@ -7,7 +7,18 @@
 #include "RenderState.h"
 
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
+
+
 using namespace CHCDemoEngine;
 using namespace std;
-
 
 
@@ -26,4 +37,5 @@
 static CGparameter sMultiplierParam;
 
+ShaderContainer SkyPreetham::sShaders;
 
 
@@ -40,6 +52,8 @@
 
 
-void SkyPreetham::Init(CGcontext context)
+void SkyPreetham::InitCG(CGcontext context)
 {	
+	ShaderProgram *pr;
+
 	sCgSkyProgram = 
 		cgCreateProgramFromFile(context, 
@@ -64,4 +78,7 @@
 
 		sMultiplierParam = cgGetNamedParameter(sCgSkyProgram, "multiplier");
+
+		pr = new ShaderProgram(sCgSkyProgram);
+		sShaders.push_back(pr);
 	}
 	else
@@ -80,7 +97,16 @@
 		cgGLLoadProgram(sCgMrtFragmentSkyDomeProgram);
 		//cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
+
+		pr = new ShaderProgram(sCgMrtFragmentSkyDomeProgram);
+		sShaders.push_back(pr);
 	}
 	else
 		cerr << "fragment skyprogram failed to load" << endl;
+}
+
+
+void SkyPreetham::ReleaseCG()
+{
+	CLEAR_CONTAINER(sShaders);
 }
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h	(revision 3021)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShaderProgram.h	(revision 3021)
@@ -0,0 +1,37 @@
+#ifndef _SHADERPROGRAM_H__
+#define _SHADERPROGRAM_H__
+
+#include "common.h"
+#include "glInterface.h"
+
+#include <Cg/cg.h>
+#include <Cg/cgGL.h>
+
+
+namespace CHCDemoEngine 
+{ 
+
+class FrameBufferObject;
+class Vector3;
+class Camera;
+class Matrix4x4;
+class ShadowMap;
+class DirectionalLight;
+
+
+class ShaderProgram
+{
+public:
+
+	ShaderProgram(CGprogram program): mProgram(program) {}
+
+	~ShaderProgram() { cgDestroyProgram(mProgram); }
+
+	CGprogram mProgram;
+};
+
+typedef  std::vector<ShaderProgram *> ShaderContainer;
+
+} // namespace 
+
+#endif // _ShaderProgram_H__
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h	(revision 3021)
@@ -4,4 +4,5 @@
 #include "common.h"
 #include "glInterface.h"
+#include "ShaderProgram.h"
 
 #include <Cg/cg.h>
@@ -20,17 +21,5 @@
 
 
-class ShaderProgram
-{
-public:
 
-	ShaderProgram(CGprogram program): mProgram(program) {}
-
-	~ShaderProgram() { cgDestroyProgram(mProgram); }
-
-	CGprogram mProgram;
-};
-
-
-typedef  std::vector<ShaderProgram *> ShaderContainer;
 typedef  std::vector<FrameBufferObject *> FBOContainer;
 
@@ -54,4 +43,7 @@
 		a smoothing factor for temporal coherence
 	*/
+
+	~DeferredRenderer();
+
 	void Render(FrameBufferObject *fbo, 
 		        const Matrix4x4 &oldProjViewMatrix, 
@@ -63,11 +55,5 @@
 				);
 
-	/** Initialises the deferred shader and loads the required shaders:
-		This function has to be called only once.
-	*/
-	static void Init(CGcontext context);
-
-	~DeferredRenderer();
-
+	
 	void SetUseTemporalCoherence(bool temporal);
 
@@ -78,4 +64,13 @@
 
 	void SetShadingMethod(SHADING_METHOD s);
+	
+	/** Initialises the deferred shader and loads the required shaders:
+		This function has to be called only once.
+	*/
+	static void InitCG(CGcontext context);
+	/** Releases all shader resources.
+	*/
+	static void ReleaseCG();
+
 	static int colorBufferIdx;
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 2963)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Bvh.cpp	(revision 3021)
@@ -1,8 +1,2 @@
-#include <queue>
-#include <stack>
-#include <fstream>
-#include <iostream>
-#include <iomanip>
-
 #include "Bvh.h"
 #include "Camera.h"
@@ -15,4 +9,21 @@
 #include "Material.h"
 #include "gzstream.h"
+
+#include <queue>
+#include <stack>
+#include <fstream>
+#include <iostream>
+#include <iomanip>
+
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.cpp	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.cpp	(revision 3021)
@@ -1,12 +1,22 @@
+#include "common.h"
+
+#ifdef _X_WINDOW_
+#include "xwindow.h"
+#endif
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
+
+#include <stdlib.h>
 #include <math.h>
 #include <stdio.h>
-#include <stdlib.h>
 #include <time.h>
-
-#include "common.h"
-
-#ifdef _X_WINDOW_
-#include "xwindow.h"
-#endif
 
 #ifndef _MSC_VER
@@ -21,4 +31,5 @@
 #include <sys/timeb.h>
 #endif
+
 
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/ShadowMapping.cpp	(revision 3021)
@@ -515,5 +515,4 @@
 	
 	Polyhedron *clippedPolyhedron = box.CalcIntersection(*p);
-
 	DEL_PTR(p);
 	
@@ -715,5 +714,4 @@
 	glPopMatrix();
 
-	
 	glDisable(GL_POLYGON_OFFSET_FILL);
 }
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 2983)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.h	(revision 3021)
@@ -4,13 +4,15 @@
 #include "glInterface.h"
 #include "common.h"
+#include "ShaderProgram.h"
+
 #include <Cg/cg.h>
 #include <Cg/cgGL.h>
 
 
-namespace CHCDemoEngine{
-class Vector3;
-class SceneEntity;
-class RenderState;
-class Camera;
+namespace CHCDemoEngine {
+	class Vector3;
+	class SceneEntity;
+	class RenderState;
+	class Camera;
 }
 
@@ -20,9 +22,11 @@
 	/// Constructor taking sky turbitity as parameter and the sky dome geometry
 	SkyPreetham(float turbitity, CHCDemoEngine::SceneEntity *skyDome);
-	virtual ~SkyPreetham();
+	
+	~SkyPreetham();
 
-	void RenderSkyDome(const CHCDemoEngine::Vector3 &sunDir, CHCDemoEngine::Camera *camera, CHCDemoEngine::RenderState *state, bool scaleToRange);
-
-	static void Init(CGcontext context);
+	void RenderSkyDome(const CHCDemoEngine::Vector3 &sunDir, 
+		               CHCDemoEngine::Camera *camera, 
+					   CHCDemoEngine::RenderState *state, 
+					   bool scaleToRange);
 
 	void ComputeFactors(const CHCDemoEngine::Vector3 &sunDir, 
@@ -35,4 +39,7 @@
 						 CHCDemoEngine::Vector3 &diffuse,
 						 bool scaleToRange) const;
+
+	static void InitCG(CGcontext context);
+	static void ReleaseCG();
 
 
@@ -53,4 +60,6 @@
 
 	float mTurbidity;
+
+	static CHCDemoEngine::ShaderContainer sShaders;
 };
 
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3020)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/chcdemo.cpp	(revision 3021)
@@ -1,4 +1,7 @@
 // chcdemo.cpp : Defines the entry point for the console application.
 //
+
+
+#include "common.h"
 
 #ifdef _CRT_SET
@@ -6,14 +9,9 @@
 	#include <stdlib.h>
 	#include <crtdbg.h>
-#endif
-
-#include "common.h"
-
-#ifdef _CRT_SET
+
 	// redefine new operator
 	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
 	#define new DEBUG_NEW
 #endif
-
 
 #include <math.h>
@@ -490,6 +488,6 @@
 	InitCg();
 
-	DeferredRenderer::Init(sCgContext);
-	SkyPreetham::Init(sCgContext);
+	DeferredRenderer::InitCG(sCgContext);
+	SkyPreetham::InitCG(sCgContext);
 
 	Vector3 cubeCenter(470.398f, 240.364f, 182.5f);
@@ -1788,4 +1786,7 @@
 		cgDestroyProgram(RenderState::sCgMrtFragmentTexProgram);
 	
+	DeferredRenderer::ReleaseCG();
+	SkyPreetham::ReleaseCG();
+
 	if (sCgContext)
 		cgDestroyContext(sCgContext);
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3020)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.cpp	(revision 3021)
@@ -14,4 +14,15 @@
 
 
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
+
+
 using namespace std;
 
@@ -62,4 +73,5 @@
 
 ShaderContainer DeferredRenderer::sShaders;
+
 
 ///////////////////////////////////////
@@ -347,18 +359,22 @@
 DeferredRenderer::~DeferredRenderer() 
 {
+	CLEAR_CONTAINER(mFBOs);
+	glDeleteTextures(1, &noiseTex);
+}
+
+
+void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
+{
+	mUseTemporalCoherence = temporal;
+}
+
+
+void DeferredRenderer::ReleaseCG()
+{
 	CLEAR_CONTAINER(sShaders);
-	CLEAR_CONTAINER(mFBOs);
-
-	glDeleteTextures(1, &noiseTex);
-}
-
-
-void DeferredRenderer::SetUseTemporalCoherence(bool temporal)
-{
-	mUseTemporalCoherence = temporal;
-}
-
-
-void DeferredRenderer::Init(CGcontext context)
+}
+
+
+void DeferredRenderer::InitCG(CGcontext context)
 {	
 	ShaderProgram *pr;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 2951)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/AxisAlignedBox3.cpp	(revision 3021)
@@ -7,4 +7,15 @@
 #include <cassert>
 #include <iostream>
+
+#ifdef _CRT_SET
+	#define _CRTDBG_MAP_ALLOC
+	#include <stdlib.h>
+	#include <crtdbg.h>
+
+	// redefine new operator
+	#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
+	#define new DEBUG_NEW
+#endif
+
 
 using namespace std;
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/src/common.h	(revision 3021)
@@ -504,14 +504,15 @@
 #define INITIAL_TRIANGLES_PER_VIRTUAL_LEAVES 1000
 
-
-}
-
-#endif
-
-
-
-
-
-
-
-
+}
+
+
+
+#endif
+
+
+
+
+
+
+
+
Index: GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj
===================================================================
--- GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 3019)
+++ GTP/trunk/App/Demos/Vis/FriendlyCulling/FriendlyCulling.vcproj	(revision 3021)
@@ -42,5 +42,5 @@
 				Optimization="0"
 				AdditionalIncludeDirectories="libs;libs/GL;libs/Devil/include;src;libs/Zlib/include;&quot;$(CG_INC_PATH)&quot;"
-				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS"
+				PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;_CRT_SET"
 				MinimalRebuild="true"
 				BasicRuntimeChecks="3"
@@ -244,4 +244,8 @@
 			</File>
 			<File
+				RelativePath=".\src\ShaderProgram.h"
+				>
+			</File>
+			<File
 				RelativePath=".\src\SkyPreetham.cpp"
 				>
