Index: GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1302)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/SubdivisionCandidate.h	(revision 1305)
@@ -26,4 +26,5 @@
 	SubdivisionCandidate(): mRenderCostDecrease(0) {};
 
+	virtual ~SubdivisionCandidate() {};
 	virtual void EvalPriority() = 0;
 	virtual int Type() const = 0;
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1302)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.h	(revision 1305)
@@ -447,5 +447,8 @@
 		{};
 
-		~VspSubdivisionCandidate() { mParentData.Clear(); }
+		~VspSubdivisionCandidate() 
+		{
+			mParentData.Clear(); 
+		}
 
 		int Type() const { return VIEW_SPACE; }
@@ -474,5 +477,4 @@
 		VspNode *mNode;
 		Vector3 mExitPoint;
-		
 		float mMaxT;
     
@@ -588,5 +590,4 @@
 	*/
 	int CastBeam(Beam &beam);
-
 
 	/** Checks if tree validity-flags are right 
@@ -778,5 +779,4 @@
 								  const int axis,
 								  float &position);
-
 
 
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1303)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.cpp	(revision 1305)
@@ -380,6 +380,8 @@
 		tFrontData.mNode->SetSubdivisionCandidate(frontCandidate); 
 		tBackData.mNode->SetSubdivisionCandidate(backCandidate);
-Debug << "here125 candidate: " << tFrontData.mNode->GetSubdivisionCandidate() << " type: " << tFrontData.mNode->GetSubdivisionCandidate()->Type() << endl;
-Debug << "here129 candidate: " << tBackData.mNode->GetSubdivisionCandidate() << " type: " << tBackData.mNode->GetSubdivisionCandidate()->Type() << endl;
+
+		Debug << "leaf: " << tFrontData.mNode << " setting f candidate: " << tFrontData.mNode->GetSubdivisionCandidate() << " type: " << tFrontData.mNode->GetSubdivisionCandidate()->Type() << endl;
+		Debug << "leaf: " << tBackData.mNode << " setting b candidate: " << tBackData.mNode->GetSubdivisionCandidate() << " type: " << tBackData.mNode->GetSubdivisionCandidate()->Type() << endl;
+		
 		tQueue.Push(frontCandidate);
 		tQueue.Push(backCandidate);
@@ -389,9 +391,10 @@
 	}
 
-
 	//-- terminate traversal
+
     if (newNode->IsLeaf())
 	{
 		//-- store additional info
+		
 		EvaluateLeafStats(tData);
         
@@ -402,5 +405,8 @@
 			CollectRays(leaf->mObjects, leaf->mVssRays);
 		}
-
+		
+		// detach subdivision candidate: this leaf is no candidate for
+		// splitting anymore
+		tData.mNode->SetSubdivisionCandidate(NULL); 
 		// detach node so it won't get deleted
 		tData.mNode = NULL;
@@ -430,5 +436,5 @@
 	const float viewSpaceVol = mVspTree->GetBoundingBox().GetVolume();
 	const float oldProp = EvalViewCellsVolume(leaf->mObjects);
-	const float oldProp2 = splitCandidate.mParentData.mProbability; //Debug << "here8 " << (oldProp - oldProp2) / viewSpaceVol << "  " << oldProp  / viewSpaceVol << " " << oldProp2  / viewSpaceVol << endl;
+	//const float oldProp2 = splitCandidate.mParentData.mProbability; Debug << "here8 " << (oldProp - oldProp2) / viewSpaceVol << "  " << oldProp  / viewSpaceVol << " " << oldProp2  / viewSpaceVol << endl;
 
 	const float oldRenderCost = oldProp * (float)leaf->mObjects.size() / viewSpaceVol;
@@ -440,5 +446,4 @@
 
 	newRenderCost /=  viewSpaceVol;
-
 	const float renderCostDecr = oldRenderCost - newRenderCost;
 
@@ -1071,7 +1076,8 @@
 					vc->Mail();
 					if (setCounter)
+					{
 						vc->mCounter = 0;
+					}
 				}
-				
 				viewCells.push_back(vc);
 			}
@@ -1106,7 +1112,9 @@
 		VspLeaf *leaf = vc->mLeaf;
 		SubdivisionCandidate *candidate = leaf->GetSubdivisionCandidate();
-		Debug << "here100 candidate: " << candidate << " type: " << candidate->Type() << endl;
 		
-		dirtyList.push_back(leaf->GetSubdivisionCandidate());
+		if (candidate) // is this leaf still a split candidate?
+		{
+			dirtyList.push_back(candidate);
+		}
 	}
 }
Index: GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1302)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/BvHierarchy.h	(revision 1305)
@@ -356,5 +356,8 @@
 		{};
 
-		~BvhSubdivisionCandidate() { mParentData.Clear(); }
+		~BvhSubdivisionCandidate() 
+		{ 
+			mParentData.Clear(); 
+		}
 
 		int Type() const { return OBJECT_SPACE; }
@@ -377,10 +380,11 @@
 		{}
 
-		
+		/// pointer to parent tree.
 		static BvHierarchy *sBvHierarchy;
-
 		/// parent data
 		BvhTraversalData mParentData;
+		/// the objects on the front of the potential split
 		ObjectContainer mFrontObjects;
+		/// the objects on the back of the potential split
 		ObjectContainer mBackObjects;
 	};
Index: GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1303)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/VspTree.cpp	(revision 1305)
@@ -709,5 +709,7 @@
 		// finally evaluate statistics for this leaf
 		EvaluateLeafStats(tData);
-
+		// detach subdivision candidate: this leaf is no candidate for
+		// splitting anymore
+		tData.mNode->SetSubdivisionCandidate(NULL); 
 		// detach node so it won't get deleted
 		tData.mNode = NULL;
@@ -2691,12 +2693,11 @@
 			{
 				leaf->Mail();
-				dirtyList.push_back(leaf->GetSubdivisionCandidate());Debug << "here87700" << endl;
-				Debug << "here166 candidate: " << leaf->GetSubdivisionCandidate() << endl;
-				Debug << "here120 candidate: " << leaf->GetSubdivisionCandidate() << " type: " << leaf->GetSubdivisionCandidate()->Type() << endl;
-				Debug << "here877" << endl;
+				if (leaf->GetSubdivisionCandidate()) // a candidate still attached to this node
+				{
+					dirtyList.push_back(leaf->GetSubdivisionCandidate());
+				}
 			}
 			break;
 		}
-		break;
 	default:
 		break;
@@ -2794,8 +2795,6 @@
 				obj->Mail();
 				obj->mCounter = 0;
-
 				++ pvsSize;
 			}
-
 			++ obj->mCounter;	
 			break;
@@ -2815,8 +2814,6 @@
 				leaf->Mail();
 				leaf->mCounter = 0;
-
 				pvsSize += (int)leaf->mObjects.size();
 			}
-
 			++ leaf->mCounter;	
 			break;
@@ -2864,5 +2861,4 @@
 		{
 			BvhLeaf *leaf = mHierarchyManager->mBvHierarchy->GetLeaf(obj);
-
 			if (!leaf->Mailed())
 			{
@@ -2928,6 +2924,5 @@
 	ray.GetSampleData(isTermination, pt, &obj, &node);
 
-	if (!obj) 
-		return 0;
+	if (!obj) return 0;
 
 	int pvs = 0;
@@ -2977,6 +2972,5 @@
 	leaf->Mail();
 
-	int pvs = 0;
-	pvs += (int)(leaf->mObjects.size() - leaf->mMultipleObjects.size());
+	int pvs = (int)(leaf->mObjects.size() - leaf->mMultipleObjects.size());
 
 	ObjectContainer::const_iterator oit, oit_end = leaf->mMultipleObjects.end();
@@ -2985,5 +2979,4 @@
 	{
 		Intersectable *obj = *oit;
-
 		if (!obj->Mailed())
 		{
@@ -2996,3 +2989,4 @@
 }
 
-}
+
+}
Index: GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1303)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/HierarchyManager.cpp	(revision 1305)
@@ -311,5 +311,4 @@
 
 	cout << "finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
-
 	mBvHierarchy->mBvhStats.Stop();
 }
@@ -399,6 +398,5 @@
 	while (!FinishedConstruction())
 	{
-		mCurrentCandidate = NextSubdivisionCandidate();
-	    
+		mCurrentCandidate = NextSubdivisionCandidate();    
 		mTotalCost -= mCurrentCandidate->GetRenderCostDecrease();
 
@@ -408,8 +406,9 @@
 		//Debug << "ratio: " << costRatio << " min ratio: " << mTermMinGlobalCostRatio << endl;
 		if (costRatio < mTermMinGlobalCostRatio)
+		{
 			++ mGlobalCostMisses;
+		}
 
 		//-- subdivide leaf node
-
 		if (ApplySubdivisionCandidate(mCurrentCandidate))
 		{
@@ -419,14 +418,9 @@
 			EvalSubdivisionStats(*mCurrentCandidate);
 		
-			// reevaluate candidates affected by the split
-			// for view space splits, this would be object space splits
-			// and other way round
+			// reevaluate candidates affected by the split for view space splits, 
+			// this would be object space splits and other way round
 			if (repair) RepairQueue();
-
-			Debug << "candidate: " << mCurrentCandidate->Type() << ", priority: " 
-				  << mCurrentCandidate->GetPriority() << endl;
 		}
 		
-		Debug << "here66 " << mCurrentCandidate << endl;
 		DEL_PTR(mCurrentCandidate);
 	}
@@ -458,6 +452,4 @@
 
 			mBvHierarchy->CollectDirtyCandidates(sc, dirtyList);
-
-sc->mParentData.mNode->SetSubdivisionCandidate(NULL); // HACK
 			break;
 		}
@@ -474,6 +466,4 @@
 
 	mVspTree->CollectDirtyCandidates(sc, dirtyList);
-
-sc->mParentData.mNode->SetSubdivisionCandidate(NULL); // HACK
 }
 
@@ -484,9 +474,8 @@
 	if (mCurrentCandidate->Type() == SubdivisionCandidate::VIEW_SPACE)
 	{
-		Debug << "collecting candidates after view space split" << endl;
 		CollectViewSpaceDirtyList(dirtyList);
 	}
 	else // object space split
-	{	Debug << "collecting candidates after object space split" << endl;
+	{
 		CollectObjectSpaceDirtyList(dirtyList);
 	}
@@ -515,35 +504,27 @@
 	// collect list of "dirty" candidates
 	vector<SubdivisionCandidate *> dirtyList;
-	Debug << "collecting dirty candidates" << endl;
 	CollectDirtyCandidates(dirtyList);
-	
+	Debug << "collected " << (int)dirtyList.size() << " candidates for reevaluation" << endl;
+
 	//-- reevaluate the dirty list
-
 	vector<SubdivisionCandidate *>::const_iterator sit, sit_end = dirtyList.end();
 	
-	Debug << "collected " << (int)dirtyList.size() << " candidates" << endl;
-
 	for (sit = dirtyList.begin(); sit != sit_end; ++ sit)
 	{
 		SubdivisionCandidate* sc = *sit;
-		float rcd = sc->GetRenderCostDecrease();
-		Debug << "here6 " << rcd << endl;
-		Debug << "here84 " << endl;
-		Debug << "here7 " << sc << endl;
-		Debug << "here90 " << sc->GetPosition() << endl;
-		Debug << "here91 " << sc->Type() << endl;
-		//Debug << "here1191" << sc->Type() << endl;
-		//Debug << "sc: " << sc << " " << sc->GetPosition() << " " << sc->Type() << endl;
+		const float rcd = sc->GetRenderCostDecrease();
 		
-		Debug << "here11" << endl;
 		mTQueue.Erase(sc); // erase from queue
-		Debug << "here2" << endl;
-
 		sc->EvalPriority(); // reevaluate
 		
-	/*	cout << "render cost decrease diff " << rcd - sc->GetRenderCostDecrease() 
-			 << " old: " << rcd << " new " << sc->GetRenderCostDecrease()
-			 << " type " << sc->Type() << endl;*/
-
+		/*
+		Debug << "candidate " << sc << " reevaluated\n" 
+			  << "render cost decrease diff " <<  rcd - sc->GetRenderCostDecrease()
+			  << " old: " << rcd << " new " << sc->GetRenderCostDecrease() << endl;*/
+		if (0)
+		{
+			const float rcDiff =  rcd - sc->GetRenderCostDecrease();
+			mTotalCost += rcDiff;
+		}
 		mTQueue.Push(sc); // reinsert
 	}
@@ -560,6 +541,5 @@
 		mOspTree->Export(stream);
 		stream << endl << "</ObjectSpaceHierarchy>" << endl;
-		break;
-		
+		break;		
 	case BV_BASED_OBJ_SUBDIV:
 		stream << "<ObjectSpaceHierarchy type=\"bvh\">" << endl;
@@ -584,5 +564,4 @@
 		// potentially visible objects
 		return vc->AddPvsSample(obj, pdf, contribution);
-
 	case KD_BASED_OBJ_SUBDIV:
 		{
@@ -681,5 +660,5 @@
 		mVspTree->PrepareConstruction(sampleRays, forcedViewSpace, *viewSpaceRays);
 	
-	// add to queue
+	// add to subdivision candidate to split candidate queue
 	mTQueue.Push(vsc);
 
@@ -708,5 +687,4 @@
 	/////////////////////////////////////////////////////////////
 	
-	
 	switch (mObjectSpaceSubdivisonType)
 	{
