occlusion culling causes flickering

this is my (unorganized, unoptimitzed and ugly i know) occlusion culling code. it works fine - except for some frames in which meshes that cover a large portion of the screen seem to flicker.

<vector> m_vpkVisibleGroups contains meshes that passed view frustum culling.
<vector> m_vpkVisibleNotOccludedGroups contains the meshes from the first array that passed occlusion culling.

	// generate query id's
	unsigned int uiCount = m_vpkVisibleGroups.size();
	unsigned int *uiQueryIds = new unsigned int[uiCount];

	glGenQueries( uiCount, &uiQueryIds[0] );
	for( unsigned int i = 0; i < m_vpkVisibleGroups.size(); i++ )
		// get group
		Group *pkGroup = m_vpkVisibleGroups[i];

		// begin query
		glBeginQuery( GL_SAMPLES_PASSED, uiQueryIds[i] );

		// render bounding box
		pkGroup->GetBoundingBox()->Render( false );

		// end query
		glEndQuery( GL_SAMPLES_PASSED );

	unsigned int uiResult = 0;

		glGetQueryObjectuiv( uiQueryIds[uiCount-1], GL_QUERY_RESULT_AVAILABLE, &uiResult );
	while( !uiResult );

	for( unsigned int i = 0; i < uiCount; i++ )
		// get result
		unsigned int uiResult;
		glGetQueryObjectuiv( uiQueryIds[i], GL_QUERY_RESULT, &uiResult );

		if( uiResult )
			m_vpkVisibleNotOccludedGroups.push_back( m_vpkVisibleGroups[i] );

	// delete query id's
	glDeleteQueries( uiCount, &uiQueryIds[0] );

any idea what’s wrong? glFlush() didn’t help.


the problem is quite easy to explain - you render all the BBs into the depth buffer - so it might appear that one of the BBs (call it A) is completely occluded by another one (B) - BUT - the actual object (B) which lies inside its BB might not occlude the actual object (A)!
Your code will not work in a 100% conservative way of you do it like this, but there are other ways to do it correctly with occlusion queries.