Solved! Go to Solution.
Rich,
I tried intantiating the cursor and filter in the GetLargest Adjacent function to begin with, it still had the same problem.
GC.Collect() GC.WaitForPendingFinalizers()
Try
Using comReleaser As ESRI.ArcGIS.ADF.ComReleaser = New ESRI.ArcGIS.ADF.ComReleaser()
Dim pFeat As IFeature = Nothing
Dim pEnumFeat As IEnumFeature = CType(pMap.FeatureSelection, IEnumFeature)
'Dim pEnumFeat As IEnumFeature = CType(pFLayer.SelectionSet, IEnumFeature)
pEnumFeat.Reset()
Dim pFCur As IFeatureCursor = Nothing
Dim pSF As ISpatialFilter
pSF = New SpatialFilter
Dim pTopoOp As ITopologicalOperator
Dim pLargestAdj As IFeature = Nothing
Dim sliverFeat As IFeature = Nothing
sliverFeat = pEnumFeat.Next
Do Until sliverFeat Is Nothing
'this is really the smallest
'---------------------------------------------------------------------
pLargestAdj = GetLargestAdjacent(sliverFeat, True, pSF, pFCur, pFClass)
'---------------------------------------------------------------------
If Not pLargestAdj Is Nothing Then
pTopoOp = CType(pLargestAdj.ShapeCopy, ITopologicalOperator)
pLargestAdj.Shape = pTopoOp.Union(sliverFeat.ShapeCopy)
updateFeature(m_pFLayer.FeatureClass, pLargestAdj.OID, pLargestAdj.Shape)
sliverFeat.Delete()
Marshal.FinalReleaseComObject(sliverFeat)
Marshal.FinalReleaseComObject(pTopoOp)
Marshal.FinalReleaseComObject(pLargestAdj)
Else
Debug.Print("nothing adjacent to: " & sliverFeat.OID)
End If
nCurRecNo = nCurRecNo + 1
My.ArcMap.Application.StatusBar.Message(0) = nCurRecNo.ToString & "/" & n.ToString & " records complete."
Application.DoEvents()
comReleaser.ManageLifetime(sliverFeat)
sliverFeat = pEnumFeat.Next
Loop
'Stop editing and save the edits
pEditor.StopEditing(True)
Runtime.InteropServices.Marshal.FinalReleaseComObject(pLargestAdj)
End Using
Catch ex As Exception
MsgBox("Error: " & ex.StackTrace, , "RElimAddinPB_onClick")
End Try
m_gdbPath = ""
'refresh the selectionset on screen
Dim pActiveView As IActiveView
pActiveView = CType(pMap, IActiveView)
pMxDoc.CurrentContentsView.Refresh(Nothing)
pActiveView.Refresh()
'Let the user know it completed successfully
MsgBox("Complete")
'Show the user how long it took to process
Dim tElapsed As TimeSpan = DateTime.Now.Subtract(tStartTime)
MsgBox(nCurRecNo.ToString + " records in: " + tElapsed.Hours.ToString + " hours " + tElapsed.Minutes.ToString + " minutes " + tElapsed.Seconds.ToString + " seconds.")
m_pFLayer = Nothing
Catch ex As Exception
MsgBox(ex.StackTrace)
MsgBox(ex.Message)
End Try
End Sub
Function GetLargestAdjacent(ByVal pFeat As IFeature, ByVal getSecondLargest As Boolean, ByVal pSF As ISpatialFilter, ByVal pFcur As IFeatureCursor, ByVal pFC As IFeatureClass) As IFeature
'MsgBox("in get largest adjacent")
Try
pSF.Geometry = pFeat.Shape
pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
'pFcur = pFC.Search(pSF, False)
pFcur = pFC.Update(pSF, False)
'If m_pFLayer.FeatureClass.FeatureCount(pSF) < 3 Then
' getSecondLargest = False
'End If
If pFC.FeatureCount(pSF) < 3 Then
getSecondLargest = False
End If
Using comReleaser As ESRI.ArcGIS.ADF.ComReleaser = New ESRI.ArcGIS.ADF.ComReleaser()
Dim pFeat2 As IFeature = Nothing, pLargestFeat As IFeature = Nothing
comReleaser.ManageLifetime(pFeat2)
comReleaser.ManageLifetime(pFcur)
Dim dMaxArea As Double = 0
pFeat2 = pFcur.NextFeature
Do Until pFeat2 Is Nothing
If Not pFeat2 Is pFeat Then
If pLargestFeat Is Nothing Then
pLargestFeat = pFeat2
dMaxArea = GetArea(CType(pFeat2.Shape, IArea))
pFcur.Flush()
'Runtime.InteropServices.Marshal.FinalReleaseComObject(pLargestFeat)
Else
If GetArea(CType(pFeat2.Shape, IArea)) > dMaxArea Then
pLargestFeat = pFeat2
dMaxArea = GetArea(CType(pFeat2.Shape, IArea))
Runtime.InteropServices.Marshal.FinalReleaseComObject(pLargestFeat)
Runtime.InteropServices.Marshal.FinalReleaseComObject(pFeat2)
End If
End If
End If
pFcur.Flush()
pFeat2 = pFcur.NextFeature
Loop
Marshal.ReleaseComObject(pFcur)
'comReleaser.Dispose()
If getSecondLargest Then
'pFCur = pFC.Search(pSF, False)
'pFcur = m_pFLayer.FeatureClass.Search(pSF, False)
pFcur = m_pFLayer.FeatureClass.Update(pSF, False)
Dim pSecondLargestFeat As IFeature = Nothing
Dim dSecondMaxArea As Double = 0
pFeat2 = pFcur.NextFeature
Do Until pFeat2 Is Nothing
comReleaser.ManageLifetime(pFcur)
If Not pFeat2 Is pLargestFeat Then
If Not pFeat2 Is pFeat Then
If pSecondLargestFeat Is Nothing Then
pSecondLargestFeat = pFeat2
dSecondMaxArea = GetArea(CType(pFeat2.Shape, IArea))
Else
If GetArea(CType(pFeat2.Shape, IArea)) > dSecondMaxArea Then
pSecondLargestFeat = pFeat2
dSecondMaxArea = GetArea(CType(pFeat2.Shape, IArea))
End If
End If
End If
End If
pFeat2 = pFcur.NextFeature
pFcur.Flush()
Loop
pLargestFeat = pSecondLargestFeat
End If
Marshal.ReleaseComObject(pFcur)
GetLargestAdjacent = pLargestFeat
'comReleaser.Dispose()
End Using
Catch ex As Exception
MsgBox(ex.Message, , "Ex routine")
MsgBox(ex.StackTrace, , "Ex routine")
GetLargestAdjacent = Nothing
End Try
End Function
Private Sub updateFeature(ByVal fc As IFeatureClass, ByVal oid As Integer, ByVal newGeom As IGeometry)
Dim queryFilter As IQueryFilter = New QueryFilter() With { _
.WhereClause = Convert.ToString(fc.OIDFieldName) & " = " & oid.ToString() _
}
Dim featureCursor As IFeatureCursor = fc.Update(queryFilter, False)
Dim featureToUpdate As IFeature = featureCursor.NextFeature()
featureToUpdate.Shape = newGeom
featureCursor.UpdateFeature(featureToUpdate)
Runtime.InteropServices.Marshal.ReleaseComObject(featureToUpdate)
featureCursor.Flush()
End Sub
Are you getting the ArcMap 'Out of memory' error or something else?
I'm not sure why you are instantiating your feature cursor and spatial filter (pFCur, pSF) outside of the GetLargestAdjacent() function. They aren't used in the main loop. Try creating them in GetLargestAdjacent() and see if that helps. It's generally a bad idea to pass feature cursors around.
I remember having memory issue with Union method in 9.3. Have you tried commenting pTopoOp.Union(sliverFeat.ShapeCopy)? This might help in narrowing down the issue.
Rich,
I tried intantiating the cursor and filter in the GetLargest Adjacent function to begin with, it still had the same problem.
GC.Collect() GC.WaitForPendingFinalizers()
Dim featureCursor As IFeatureCursor = featureClass.Search(queryFilter, False) Dim feature As IFeature = featureCursor.NextFeature Do While feature IsNot Nothing ' do something with the feature FinalReleaseComObject(feature) ' release the feature before setting it to the next feature in the cursor feature = featureCursor.NextFeature Loop FinalReleaseComObject(featureCursor) ' release the cursor