Hi All,
I'm calling the function below several thousand times, but it just seems to increase memory usage until it falls over. I've tried Marshal.ReleaseComObject and ComReleaser without any luck. It seems to be related to the buffered geometry as if I give the non buffered geometry (SiteGeometry) to pSpatialFilter it seems fine.
Any ideas how i release the memory used by this function?
thanks,
Andy
Public Function doSearch(ByVal SiteGeometry As IGeometry, ByVal SearchLayer As IFeatureClass, ByVal Buffer As Integer, ByVal shapeField As String) As Integer
Dim topoOP As ITopologicalOperator2
Dim pSpatialFilter As ISpatialFilter = New SpatialFilter
topoOP = SiteGeometry
topoOP.Simplify()
Dim pGeometryBuff As IGeometry = topoOP.Buffer(Buffer)
With pSpatialFilter
.Geometry = pGeometryBuff
.GeometryField = shapeField
.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
End With
Dim theRowcount As Integer = SearchLayer.FeatureCount(pSpatialFilter)
topoOP = Nothing
pSpatialFilter = Nothing
pGeometryBuff = Nothing
pBC = Nothing
Return theRowcount
End Function
Your code looks OK to me. Don't know if this will help but maybe you can pass in siteGeometry and SearchLayer as "ByRef" rather than "ByVal"?
I would try using the SelectionSet method and getting the count off of that. Don't forget to call FinalReleaseComObject on selSet.
IWorkspace ws = ((IDataset)SearchLayer).Workspace;
ISelectionSet selSet = SearchLayer.Select(pSpatialFilter, esriSelectionType.esriSelectionTypeIDSet, esriSelectionOption.esriSelectionOptionNormal, ws);
int rowCount = selSet.Count;
Marshal.FinalReleaseComObject(selSet);
Good luck.
thanks for your suggestions - i eventually managed to reduce the memory leak to an acceptable trickle using comReleaser. Final code is below:
Public Function doSearch(ByVal SiteGeometry As IGeometry, ByVal SearchLayer As IFeatureClass, ByVal Buffer As Integer, ByVal shapeField As String, ByVal thewhere As String) As Boolean
Using comReleaser As ComReleaser = New ComReleaser()
Dim topoOperator As ITopologicalOperator = CType(SiteGeometry, ITopologicalOperator)
comReleaser.ManageLifetime(topoOperator)
Dim buffer2 As IGeometry
If Buffer = 0 Then
buffer2 = SiteGeometry
Else
buffer2 = topoOperator.Buffer(Buffer)
End If
comReleaser.ManageLifetime(buffer2)
Dim spatialFilter As ISpatialFilter = New SpatialFilterClass()
comReleaser.ManageLifetime(spatialFilter)
spatialFilter.Geometry = buffer2
spatialFilter.GeometryField = shapeField
spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
spatialFilter.WhereClause = thewhere
' Execute the query.
Dim featureCursor As IFeatureCursor = SearchLayer.Search(spatialFilter, True)
comReleaser.ManageLifetime(featureCursor)
Dim feature As IFeature = featureCursor.NextFeature()
comReleaser.ManageLifetime(feature)
If feature Is Nothing Then
Return False
Else
Return True
End If
End Using
End Function