Count features intersecting buffered geometry - memory leak

12-01-2014 10:57 AM
New Contributor II

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?



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


        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

0 Kudos
3 Replies
MVP Frequent Contributor

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"?

0 Kudos
Occasional Contributor

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;


Good luck.

0 Kudos
New Contributor II

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)


            Dim buffer2 As IGeometry

            If Buffer = 0 Then

                buffer2 = SiteGeometry


                buffer2 = topoOperator.Buffer(Buffer)

            End If


            Dim spatialFilter As ISpatialFilter = New SpatialFilterClass()


            spatialFilter.Geometry = buffer2

            spatialFilter.GeometryField = shapeField

            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects

            spatialFilter.WhereClause = thewhere

            ' Execute the query.

            Dim featureCursor As IFeatureCursor = SearchLayer.Search(spatialFilter, True)


            Dim feature As IFeature = featureCursor.NextFeature()


            If feature Is Nothing Then

                Return False


                Return True

            End If

        End Using

    End Function

0 Kudos