0

I am developing a simple add-in that will trigger an action after user selects features on the map (the action will essentially make sure a dockable window is open and in that window display information about the selected features).

I have implemented a simple function that loops over selected features using IEnumFeature and collects their OIDs. To make sure the OIDs are present for all features in the selection, I set the IEnumFeatureSetup.AllFields = true.

Now to the problem. After I've selected a number features several times (regardless of what features or how many I select), the SelectionChanged event stops from being fired. It appears as if the add-in has crashed, but no error message is displayed. This happens both in debug and production. If I try to restart the add-in by de-selecting it in the Customise - Extensions menu, I can no longer select it back. ArcMap works fine otherwise. To get the add-in to work again I must restart ArcMap.

The features come from a file geodatabase. They have 5 short text fields each and there is around 1300 of them. The more features I select at once, the less rounds of feature selection it takes for the add-in to crash.

Unlike in ArcGIS crashes with no error number in Visual Studio I get no ArcGIS error or exception!

I am developing in Arcobjects 10.1 SDK with Visual Studio 10 for C# Express using .NET 3.5. I cannot upgrade nor can I use .NET 4.0 due to client constraints. The add-in is being tested in ArcMap 10.1.

private void SelectionChanged_Handler() {

    List<string> locodes = new List<string>();

    try {
        ISelection selection = ArcMap.Document.FocusMap.FeatureSelection;
        IEnumFeature featuresEnum = (IEnumFeature)selection;
        IEnumFeatureSetup featureSetup = (IEnumFeatureSetup)selection;

        featureSetup.AllFields = true;

        IFeature feature;

        while ((feature = featuresEnum.Next()) != null) {

            int locodeFldID = feature.Fields.FindField("locode");
            string locode = feature.get_Value(locodeFldID).ToString();
            locodes.Add(locode);

            // Release feature object
            Marshal.FinalReleaseComObject(feature);
        }

        // Release IEnum reference
        Marshal.FinalReleaseComObject(featuresEnum);

        } catch (Exception e) {
            System.Windows.Forms.MessageBox.Show(e.Message);
        }
    }
}

Why is the add-in crashing (stopping to work) without apparent error. Might this be related to resources not being freed correctly?

  • Nothing looks obviously wrong here, though I'm translating to VB. Have you determined for sure that the error is in this code you've posted? I assume that after this there is code that updates the window with the locodes list? It might be a good idea to isolate this code from anything to do with the dockable window and see if it still breaks. Try putting a message box within that "try" but outside of the "while" that tells you the locode count, and do not touch the dockable window. If, doing that, you can select indefinitely, then you know that the problem is likely someplace else. – Dan Jurgella Jun 11 '15 at 1:00
  • Thanks for the suggestions, @DanJurgella. If I remove the code that aggregates the LOCODEs into a collection (i.e. I fetch the LOCODE value from the feature, do nothing with it and release the feature) I seem to be able to select "indefinitely". As soon as I start aggregating data into a collection upon selecting features, the lifetime of the add-in becomes limited. What puzzles me is that I get no exception or application error when the add-in stops working. Expanding the scope of try..catch doesn't have any effect, unfortunately. – David Jun 11 '15 at 9:30
  • Could it have something to do with where Marshal.FinalReleaseComObject(feature); is in the code? If it is within that loop it would seem to be forcing release of an object that is still needed. Have you tried it without the com release? stackoverflow.com/questions/3937181/… – Dan Jurgella Jun 11 '15 at 12:44
  • Yes, I have. Releasing the feature object inside the loop actually prolongs the lifetime of the add-in, i.e. I can re-select features more times than I would be able to without the release. This is why I originally thought I'm experiencing a memory leak, but inspecting memory allocation to ArcMap using Process Explorer hasn't confirmed that. – David Jun 11 '15 at 13:36
  • For what it's worth, I was able to create an extension using your code above, but without the com release, and select features with no issue. I put a message box outside the "while" that tells me the feature count so I know the extension continues working. My guess is that the problem is in another part of your code. – Dan Jurgella Jun 11 '15 at 16:07
1

The problem, it seems, is in the repeated call to ArcMap.Document.FocusMap.FeatureSelection. If I store the focus map as a member variable IMap map upon initializing the extension and retrieve the selection features by calling map.FeatureSelection instead, all seems to work fine and I am able to re-select features "indefinitely".

Sadly, I am not able to explain why this works. Perhaps someone more funded than me could comment on my answer and elaborate on the difference in the two approaches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.