diff --git a/indra/newview/llfloatermodelpreview.cpp b/indra/newview/llfloatermodelpreview.cpp
index cc2a8c8aac401ed20f2740a38b8741493c5ac622..dde470693c9922775794861b21e943ae770eac27 100755
--- a/indra/newview/llfloatermodelpreview.cpp
+++ b/indra/newview/llfloatermodelpreview.cpp
@@ -2205,9 +2205,12 @@ U32 LLModelPreview::calcResourceCost()
 
 	rebuildUploadData();
 
-	if ( mModelLoader->getLoadState() != LLModelLoader::ERROR_PARSING )
+	if (mFMP && mModelLoader)
 	{
-		mFMP->childEnable("ok_btn");
+		if ( mModelLoader->getLoadState() != LLModelLoader::ERROR_PARSING )
+		{
+			mFMP->childEnable("ok_btn");
+		}
 	}
 
 	U32 cost = 0;
@@ -2215,7 +2218,7 @@ U32 LLModelPreview::calcResourceCost()
 	U32 num_points = 0;
 	U32 num_hulls = 0;
 
-	F32 debug_scale = mFMP->childGetValue("import_scale").asReal();
+	F32 debug_scale = mFMP ? mFMP->childGetValue("import_scale").asReal() : 1.f;
 
 	F32 streaming_cost = 0.f;
 	F32 physics_cost = 0.f;
@@ -2270,9 +2273,7 @@ U32 LLModelPreview::calcResourceCost()
 		}
 	}
 
-	//mFMP->childSetTextArg(info_name[LLModel::LOD_PHYSICS], "[HULLS]", llformat("%d",num_hulls));
-	//mFMP->childSetTextArg(info_name[LLModel::LOD_PHYSICS], "[POINTS]", llformat("%d",num_points));
-	F32 scale = mFMP->childGetValue("import_scale").asReal()*2.f;
+	F32 scale = mFMP ? mFMP->childGetValue("import_scale").asReal()*2.f : 2.f;
 
 	mDetailsSignal(mPreviewScale[0]*scale, mPreviewScale[1]*scale, mPreviewScale[2]*scale, streaming_cost, physics_cost);
 
diff --git a/indra/newview/llspatialpartition.cpp b/indra/newview/llspatialpartition.cpp
index 5f23908edad47ff54bc4b73968a42e24aade23b0..be8f81fc62964ff78ed29d6e3433422fb6698e39 100644
--- a/indra/newview/llspatialpartition.cpp
+++ b/indra/newview/llspatialpartition.cpp
@@ -64,6 +64,12 @@ const F32 SG_OCCLUSION_FUDGE = 0.25f;
 
 static U32 sZombieGroups = 0;
 U32 LLSpatialGroup::sNodeCount = 0;
+
+#define LL_TRACK_PENDING_OCCLUSION_QUERIES 0
+
+std::set<GLuint> LLSpatialGroup::sPendingQueries;
+
+
 BOOL LLSpatialGroup::sNoDelete = FALSE;
 
 static F32 sLastMaxTexPriority = 1.f;
@@ -81,6 +87,9 @@ class LLOcclusionQueryPool : public LLGLNamePool
 
 	virtual void releaseName(GLuint name)
 	{
+#if LL_TRACK_PENDING_OCCLUSION_QUERIES
+		LLSpatialGroup::sPendingQueries.erase(name);
+#endif
 		glDeleteQueriesARB(1, &name);
 	}
 };
@@ -361,9 +370,15 @@ LLSpatialGroup::~LLSpatialGroup()
 	
 	sNodeCount--;
 
-	if (gGLManager.mHasOcclusionQuery && mOcclusionQuery[LLViewerCamera::sCurCameraID])
+	if (gGLManager.mHasOcclusionQuery)
 	{
-		sQueryPool.release(mOcclusionQuery[LLViewerCamera::sCurCameraID]);
+		for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; ++i)
+		{
+			if (mOcclusionQuery[i])
+			{
+				sQueryPool.release(mOcclusionQuery[i]);
+			}
+		}
 	}
 
 	mOcclusionVerts = NULL;
@@ -1096,12 +1111,23 @@ void LLSpatialGroup::setOcclusionState(U32 state, S32 mode)
 			for (U32 i = 0; i < LLViewerCamera::NUM_CAMERAS; i++)
 			{
 				mOcclusionState[i] |= state;
+
+				if ((state & DISCARD_QUERY) && mOcclusionQuery[i])
+				{
+					sQueryPool.release(mOcclusionQuery[i]);
+					mOcclusionQuery[i] = 0;
+				}
 			}
 		}
 	}
 	else
 	{
 		mOcclusionState[LLViewerCamera::sCurCameraID] |= state;
+		if ((state & DISCARD_QUERY) && mOcclusionQuery[LLViewerCamera::sCurCameraID])
+		{
+			sQueryPool.release(mOcclusionQuery[LLViewerCamera::sCurCameraID]);
+			mOcclusionQuery[LLViewerCamera::sCurCameraID] = 0;
+		}
 	}
 }
 
@@ -1540,15 +1566,31 @@ void LLSpatialGroup::checkOcclusion()
 		{	//otherwise, if a query is pending, read it back
 
 			GLuint available = 0;
-			glGetQueryObjectuivARB(mOcclusionQuery[LLViewerCamera::sCurCameraID], GL_QUERY_RESULT_AVAILABLE_ARB, &available);
+			if (mOcclusionQuery[LLViewerCamera::sCurCameraID])
+			{
+				glGetQueryObjectuivARB(mOcclusionQuery[LLViewerCamera::sCurCameraID], GL_QUERY_RESULT_AVAILABLE_ARB, &available);
+			}
+			else
+			{
+				available = 1;
+			}
+
 			if (available)
 			{ //result is available, read it back, otherwise wait until next frame
 				GLuint res = 1;
 				if (!isOcclusionState(DISCARD_QUERY) && mOcclusionQuery[LLViewerCamera::sCurCameraID])
 				{
 					glGetQueryObjectuivARB(mOcclusionQuery[LLViewerCamera::sCurCameraID], GL_QUERY_RESULT_ARB, &res);	
+#if LL_TRACK_PENDING_OCCLUSION_QUERIES
+					sPendingQueries.erase(mOcclusionQuery[LLViewerCamera::sCurCameraID]);
+#endif
 				}
-
+				else if (mOcclusionQuery[LLViewerCamera::sCurCameraID])
+				{ //delete the query to avoid holding onto hundreds of pending queries
+					sQueryPool.release(mOcclusionQuery[LLViewerCamera::sCurCameraID]);
+					mOcclusionQuery[LLViewerCamera::sCurCameraID] = 0;
+				}
+				
 				if (isOcclusionState(DISCARD_QUERY))
 				{
 					res = 2;
@@ -1621,6 +1663,13 @@ void LLSpatialGroup::doOcclusion(LLCamera* camera)
 					}
 #if !LL_DARWIN					
 					U32 mode = gGLManager.mHasOcclusionQuery2 ? GL_ANY_SAMPLES_PASSED : GL_SAMPLES_PASSED_ARB;
+#else
+					U32 mode = GL_SAMPLES_PASSED_ARB;
+#endif
+					
+#if LL_TRACK_PENDING_OCCLUSION_QUERIES
+					sPendingQueries.insert(mOcclusionQuery[LLViewerCamera::sCurCameraID]);
+#endif
 
 					glBeginQueryARB(mode, mOcclusionQuery[LLViewerCamera::sCurCameraID]);					
 					
@@ -1637,7 +1686,7 @@ void LLSpatialGroup::doOcclusion(LLCamera* camera)
 					}
 
 					glEndQueryARB(mode);
-#endif					
+
 					if (use_depth_clamp)
 					{
 						glDisable(GL_DEPTH_CLAMP);
diff --git a/indra/newview/llspatialpartition.h b/indra/newview/llspatialpartition.h
index 85fd66b2977e3fc7c1af05b6946ab11b9f1d8f5c..664d957e498b3ce5480afbd363837cfb9e964a3f 100644
--- a/indra/newview/llspatialpartition.h
+++ b/indra/newview/llspatialpartition.h
@@ -187,6 +187,7 @@ class LLSpatialGroup : public LLOctreeListener<LLDrawable>
 		return *this;
 	}
 
+	static std::set<GLuint> sPendingQueries; //pending occlusion queries
 	static U32 sNodeCount;
 	static BOOL sNoDelete; //deletion of spatial groups and draw info not allowed if TRUE
 
diff --git a/indra/newview/llviewerwindow.cpp b/indra/newview/llviewerwindow.cpp
index dcfac012f6d4e344138e29c4d545014fce4ab975..db48b769e49c02d942e348615633a149028082eb 100644
--- a/indra/newview/llviewerwindow.cpp
+++ b/indra/newview/llviewerwindow.cpp
@@ -497,6 +497,12 @@ class LLDebugText
 			
 			ypos += y_inc;
 
+			if (!LLSpatialGroup::sPendingQueries.empty())
+			{
+				addText(xpos,ypos, llformat("%d Queries pending", LLSpatialGroup::sPendingQueries.size()));
+				ypos += y_inc;
+			}
+
 
 			addText(xpos,ypos, llformat("%d Avatars visible", LLVOAvatar::sNumVisibleAvatars));