ORIGINAL POST:
I'm not actually sure whether this is a VB.net problem or an ESRI problem...but given the number of really odd ESRI quirks I've encountered, my money's on the latter...
I have a piece of code in an Add-In toolbar that creates an update cursor from a spatial query and cycles through the results checking feature length and deleting features below a certain threshold. I don't think that's too relevant, but just in case...
My problem is that when I run the code independently of the IDE (i.e., just from ArcMap) OR when I run the code from the IDE (Debug-->Start Debugging) without breakpoints, this block of code does not execute. If, however, I place a breakpoint after the cursor is retrieved and run the code from the IDE, this block of code executes just fine. The results are HUGELY different depending on whether I've put a breakpoint there or not.
Has anyone encountered a situation like this? Any ideas of what I might be doing wrong? I've tried deleting the add-in, cleaning the solution, restarting my computer, and rebuilding, and it does not help.
Thanks!
Edit: Here's the code block:
curSubs = fcSubBasin.Update(Nothing, False)
pComReleaser2.ManageLifetime(curSubs)
ftrSub = curSubs.NextFeature
Dim pOutline As ESRI.ArcGIS.Geometry.IPolyline, pTopoOper As ESRI.ArcGIS.Geometry.ITopologicalOperator
Do While Not ftrSub Is Nothing
Using pComReleaser3 As New ESRI.ArcGIS.ADF.ComReleaser
curStreams = modUtilities.PerformSpatialQuery(fcStreams, ftrSub.Shape, ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelContains)
pComReleaser3.ManageLifetime(curStreams)
'figure out which is the longest
pDataStats = New ESRI.ArcGIS.Geodatabase.DataStatistics
pDataStats.SimpleStats = True
pDataStats.Field = strLengthFld
pDataStats.Cursor = curStreams
pStats = pDataStats.Statistics
dblMaxLength = pStats.Maximum
curStreams = Nothing
End Using
Using pComReleaser3 As New ESRI.ArcGIS.ADF.ComReleaser
'first, delete all straggler segments - little bits of stream that are a result of the delineated boundary not perfectly matching
'the nodes of the stream network
curStreams = modUtilities.PerformSpatialQuery(fcStreams, ftrSub.Shape, ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelContains, method:="Update")
pComReleaser3.ManageLifetime(curStreams)
ftrStream = curStreams.NextFeature
Do Until ftrStream Is Nothing
'only keep the feature if it's the max length we just figured out
If ftrStream.Value(intLengthFld) < dblMaxLength Then
curStreams.DeleteFeature()
End If
ftrStream = curStreams.NextFeature
Loop
curStreams = Nothing
End Using
Using pComReleaser3 As New ESRI.ArcGIS.ADF.ComReleaser
'next, delete any segments that happen to coincide perfectly with the subwatershed boundaries; they were created by intersect but would
'not have been caught by the polygon contains above
pTopoOper = TryCast(ftrSub.Shape, ESRI.ArcGIS.Geometry.ITopologicalOperator)
If Not pTopoOper Is Nothing Then
pOutline = pTopoOper.Boundary
curStreams = modUtilities.PerformSpatialQuery(fcStreams, pOutline, ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum.esriSpatialRelContains, method:="Update")
pComReleaser3.ManageLifetime(curStreams)
ftrStream = curStreams.NextFeature
Do Until ftrStream Is Nothing
'delete any stream segments that coincide with the subwatershed boundary
curStreams.DeleteFeature()
ftrStream = curStreams.NextFeature
Loop
curStreams = Nothing
End If
ftrSub = curSubs.NextFeature
End Using
Loop
curSubs = Nothing
REVISED INFORMATION: After further investigation I've discovered that what's actually happening is that while I'm stepping through the debugger, the spatial query works properly. When I am not stepping through the debugger, it does not. I am using ESRI's spatial query snippet as below (with very small tweaks), passing relation 'contains' to select line segments that are within polygons. The line feature class is the result of an intersection with the polygon feature class immediately prior to this, so all lines should fall within a polygon. However, when I am not stepping through with the debugger, several of the lines are not selected. Does this clarification offer any additional ideas?
Public Function PerformSpatialQuery(ByVal featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, _
ByVal searchGeometry As ESRI.ArcGIS.Geometry.IGeometry, _
ByVal spatialRelation As ESRI.ArcGIS.Geodatabase.esriSpatialRelEnum, _
Optional ByVal whereClause As System.String = "", _
Optional ByVal method As String = "Search") As ESRI.ArcGIS.Geodatabase.IFeatureCursor
' create a spatial query filter
Dim spatialFilter As ESRI.ArcGIS.Geodatabase.ISpatialFilter = New ESRI.ArcGIS.Geodatabase.SpatialFilterClass()
' specify the geometry to query with
spatialFilter.Geometry = searchGeometry
' specify what the geometry field is called on the Feature Class that we will be querying against
Dim nameOfShapeField As System.String = featureClass.ShapeFieldName
spatialFilter.GeometryField = nameOfShapeField
' specify the type of spatial operation to use
spatialFilter.SpatialRel = spatialRelation
' create the where statement
If whereClause <> "" Then 'added the 'optional' in the function statement and this if-then to make the whereClause optional
spatialFilter.WhereClause = whereClause
End If
' perform the query and use a cursor to hold the results
Dim featureCursor As ESRI.ArcGIS.Geodatabase.IFeatureCursor
If String.Equals(method, "Search", StringComparison.CurrentCultureIgnoreCase) Then
featureCursor = featureClass.Search(spatialFilter, False)
Else
featureCursor = featureClass.Update(spatialFilter, False)
End If
Return featureCursor
End Function
-
I've just discovered that if I put in a message box at the breakpoint location, the code will also run (with the breakpoint removed), if that helps with diagnosis!BeckyZ– BeckyZ2013年01月07日 18:08:32 +00:00Commented Jan 7, 2013 at 18:08
-
Posting the function that causes the error would help.Rich Wawrzonek– Rich Wawrzonek2013年01月07日 18:29:55 +00:00Commented Jan 7, 2013 at 18:29
-
When trying to debug programs, I've seen this occur with uninitialized variables/values not set correctly. They're handled differently depending on whether the code is run via exe or via the debugger.mkennedy– mkennedy2013年01月07日 18:55:48 +00:00Commented Jan 7, 2013 at 18:55
-
Is this just one large script in the OnClick event? Where is the feature workspace opened?Rich Wawrzonek– Rich Wawrzonek2013年01月07日 18:59:13 +00:00Commented Jan 7, 2013 at 18:59
-
Chances that the code executes differently without breakpoints are next to zero. Wrap the whole snippet in a try-catch construct and put the breakpoint into the catch block to see what's happening and why. You may not be seeing any error since commands, tools etc. generally do not propagate any unhandled exceptions to the user.Petr Krebs– Petr Krebs2013年01月07日 19:25:22 +00:00Commented Jan 7, 2013 at 19:25
1 Answer 1
Add a Try Catch block around the problematic code and report the error message in a messagebox. This could be a memory issue related to your update cursor. Make sure you are properly releasing your feature and update cursor objects using the COM releaser.
Before each case where you are calling ftrStream = curStreams.NextFeature try adding this line of code:
System.Runtime.InteropServices.Marshal.ReleaseComObject(ftrStream)
Based on your additional info I would also try putting the code to create the feature cursor inside your main function. I really try to avoid passing cursors between functions in my programs. You might be having an object go out of scope when it runs in real-time, possibly due to .net garbage collection. This is probably a problem in your design by passing around references to feature classes, cursors, etc. Managing the lifetime of curStreams with three instance of ComReleaser in the same routine is not good.
-
Thanks for your suggestion. I'm not actually getting an error - it's just that I can tell this particular block of code isn't running based on the results I'm getting (and when I try to step through with the debugger to see where it's going wrong, it ends up running just fine and giving the proper results). I am using the COM releaser on the cursor.BeckyZ– BeckyZ2013年01月07日 18:44:54 +00:00Commented Jan 7, 2013 at 18:44
-
Thanks for your additional suggestion - I have implemented as you suggested but I am still encountering a difficulty.BeckyZ– BeckyZ2013年01月07日 22:54:36 +00:00Commented Jan 7, 2013 at 22:54