Using comRel As New ComReleaser() pFeatureCursor = pFeatureClass.Update(pQueryFilter, False) comRel.ManageLifetime(pFeatureCursor) pFeature = pFeatureCursor.NextFeature Do While Not pFeature Is Nothing ' Do something pFeatureCursor.UpdateFeature(pFeature) pFeature = pFeatureCursor.NextFeature Loop pFeatureCursor.Flush() End Using
Using comRel As New ComReleaser() pFeatureCursor = pFeatureClass.Update(pQueryFilter, False) comRel.ManageLifetime(pFeatureCursor) pFeature = pFeatureCursor.NextFeature Do While Not pFeature Is Nothing ' Do something Using comRel2 as New ComReleaser() pFeatureCursor2 = pFeatureClass2.Search(pQueryFilter2, False) comRel2.ManageLifetime(pFeatureCursor2) ' Do something inner looping stuff End Using pFeatureCursor.UpdateFeature(pFeature) pFeature = pFeatureCursor.NextFeature Loop pFeatureCursor.Flush() End Using
Using comRel As New ComReleaser() pFeatureCursor = pFeatureClass.Update(pQueryFilter, False) comRel.ManageLifetime(pFeatureCursor) pFeature = pFeatureCursor.NextFeature Do While Not pFeature Is Nothing ' Do something pFeatureCursor2 = pFeatureClass2.Search(pQueryFilter2, False) comRel.ManageLifetime(pFeatureCursor2) ' Do something inner looping stuff End Using pFeatureCursor.UpdateFeature(pFeature) pFeature = pFeatureCursor.NextFeature Loop pFeatureCursor.Flush() End Using
Solved! Go to Solution.
Same issue in Java. In Java, we can call garbage collection methods, but garbage collection will run when it wants according to whatever algorithm is uses.
So you're saying that your code will not wait for garbage collection, why is that? Because of the nested cursors it is running slow, or ?
Private Sub GetRoutesList() ' Set Up Query Filter for the Full Date Range of the Collision Layer respecting PDO and DUI flags Dim pQF As IQueryFilter = New QueryFilterClass pQF.WhereClause = "NOT " & fldCollisionDate & " IS NULL" & whereSeverity & whereDUI pQF.SubFields = fldCollisionDate & ", " & fldPrimaryRoad & ", " & fldRID & ", " & fldSeverity & ", party_sobriety_1, party_sobriety_2" ' Query Collisions Date Range and Street to fill Route Combobox List pQF.WhereClause = fldCollisionDate & " >= date '" & m_FromDate.ToString("yyyy-MM-dd HH:mm:ss") & "' AND " & fldCollisionDate & " <= date '" & m_ToDate.ToString("yyyy-MM-dd HH:mm:ss") & "' AND " & fldRID & " > ' ' AND Upper(" & fldPrimaryRoad & ") = '" & m_strPrimary & "'" & whereSeverity & whereDUI Dim dataStatisticsRoutes As IDataStatistics = New DataStatisticsClass Dim featCursorRouteList As IFeatureCursor = m_FLCollisions.Search(pQF, True) ' Use DataStatistics to get Unique Values dataStatisticsRoutes.Field = fldRID dataStatisticsRoutes.Cursor = featCursorRouteList Dim pEnum As System.Collections.IEnumerator = dataStatisticsRoutes.UniqueValues() ' Fill the Combobox and suspend refresh until list is populated cboRoutes.BeginUpdate() ' Clear the Routes Combobox cboRoutes.Items.Clear() ' Reset the enumerator before trying to access its values pEnum.Reset() ' Read each Unique value and populate the Route list. Sorting is handled by a Combobox property. For i As Int32 = 0 To dataStatisticsRoutes.UniqueValueCount - 1 pEnum.MoveNext() cboRoutes.Items.Add(UCase(pEnum.Current())) Next i ' Release the QueryFilter If Not pQF Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(pQF) End If ' Release the dataStatistics If Not dataStatisticsRoutes Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(dataStatisticsRoutes) End If ' Release the Cursor If Not featCursorRouteList Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(featCursorRouteList) End If ' Allow the Combobox to refresh cboRoutes.EndUpdate() If cboRoutes.Items.Contains(m_strRoute) Then ' Set Combobox Text to the current Map Route cboRoutes.Text = m_strRoute ElseIf cboRoutes.Items.Count > 0 Then ' Set Combobox Text to First Route in the List cboRoutes.Text = cboRoutes.Items(0) m_strRoute = cboRoutes.Text End If End Sub
I wrote an Add-In that used a Windows form and many chained functions that create and release cursors using ReleaseComObject. Two of the functions use IDataStatistics, which takes a populated cursor as a parameter. When I was targeting ArcGIS 10.0 it all worked fine. Now with 10.1 I am getting a lot of errors about trying to access protected memory, which the MSDN help suggests is due to memory corruption, usually related to unmanaged code. The error help suggests that the memory corruption may be accumulating through many operations before triggering the error.
The failure seems to always happen on the second or third pass that is triggered by user form updates and occurs within the subroutines that have the cursor that feeds to an IDataStatistics object. The error either appears when I call the UniqueValues method of the IDataStatistics object or when I do a Search on the feature cursor before it is passed to the IDataStatistics object (only on the second or third time through the code, never the first). Its behavior can be altered by the use or lack of use of the ReleaseComObject statements for the cursor or the IDataStatistics. I have tried all of the combinations I can think of and orders of the releaser. Below is a sample of how the code is written with the two lines where the protected memory error can occur shown in Red. Any suggestions on what objects I should use the ReleaseComObject on and the order or placement of that code or how to trap these errors and issolate the real cause?
Private Sub GetRoutesList() ' Set Up Query Filter for the Full Date Range of the Collision Layer respecting PDO and DUI flags Dim pQF As IQueryFilter = New QueryFilterClass pQF.WhereClause = "NOT " & fldCollisionDate & " IS NULL" & whereSeverity & whereDUI pQF.SubFields = fldCollisionDate & ", " & fldPrimaryRoad & ", " & fldRID & ", " & fldSeverity & ", party_sobriety_1, party_sobriety_2" ' Query Collisions Date Range and Street to fill Route Combobox List pQF.WhereClause = fldCollisionDate & " >= date '" & m_FromDate.ToString("yyyy-MM-dd HH:mm:ss") & "' AND " & fldCollisionDate & " <= date '" & m_ToDate.ToString("yyyy-MM-dd HH:mm:ss") & "' AND " & fldRID & " > ' ' AND Upper(" & fldPrimaryRoad & ") = '" & m_strPrimary & "'" & whereSeverity & whereDUI Dim dataStatisticsRoutes As IDataStatistics = New DataStatisticsClass Dim featCursorRouteList As IFeatureCursor = m_FLCollisions.Search(pQF, True) ' Use DataStatistics to get Unique Values dataStatisticsRoutes.Field = fldRID dataStatisticsRoutes.Cursor = featCursorRouteList Dim pEnum As System.Collections.IEnumerator = dataStatisticsRoutes.UniqueValues() ' Fill the Combobox and suspend refresh until list is populated cboRoutes.BeginUpdate() ' Clear the Routes Combobox cboRoutes.Items.Clear() ' Reset the enumerator before trying to access its values pEnum.Reset() ' Read each Unique value and populate the Route list. Sorting is handled by a Combobox property. For i As Int32 = 0 To dataStatisticsRoutes.UniqueValueCount - 1 pEnum.MoveNext() cboRoutes.Items.Add(UCase(pEnum.Current())) Next i ' Release the QueryFilter If Not pQF Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(pQF) End If ' Release the dataStatistics If Not dataStatisticsRoutes Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(dataStatisticsRoutes) End If ' Release the Cursor If Not featCursorRouteList Is Nothing Then System.Runtime.InteropServices.Marshal.ReleaseComObject(featCursorRouteList) End If ' Allow the Combobox to refresh cboRoutes.EndUpdate() If cboRoutes.Items.Contains(m_strRoute) Then ' Set Combobox Text to the current Map Route cboRoutes.Text = m_strRoute ElseIf cboRoutes.Items.Count > 0 Then ' Set Combobox Text to First Route in the List cboRoutes.Text = cboRoutes.Items(0) m_strRoute = cboRoutes.Text End If End Sub
There are many other cursor calls that do not involve IDataStatistics and other Com objects from ArcGIS being created in the code. None of the cursors are embedded in each other and I have always used the ReleaseComObject call after I finish reading the cursor. These cursors have not generated errors so far, but I need to be sure they are not contributing to the problem. Any suggestions are appreciated.
Richard,
Did you see this thread that discovered the bug NIM087476 �??Memory leakage ESRI.ArcGIS.Geodatabase.IDataStatistics using UniqueValues property with ArcGIS 10.1 with SP1�?�