0

I have a recycling cursor which returns a bunch of features from a featureClass:

while((feature = cursor.NextFeature) != null)
{
    // do something with the feature
}

As ArcGIS will use the same COM-objekt again and again when setting recycling to true I don´t need to release this com-object within my loop.

However when the last iteration executes the COM-object will stay alive as it is not overriden by a new feature. But as feature is null we don´t have a reference to this object making it impossible for us to release it.

So how do we release the (only) COM-object when using a recycling cursor? Of course I could count the features returned from the cursor beforehand and increase a counter in my loop indicating if we´re handling the very last one or not. But I find this quite annoying and in particular overkill as it queries the DB unneccessarily.

Also calling GC.Collect seems odd to me as MSDN cleary states explicitely calling the GC is not recommended as it is quite time-consuming.

0

You are using the ComReleaser class, right? GC.Collect is indeed not necessary.

Since the feature is reused, you might as well manage the first feature, e.g.

using (var releaser = new ComReleaser())
{
    var cursor = ....
    releaser.ManageLifetime(cursor);

    var isManaged = false;
    while((feature = cursor.NextFeature) != null)
    {
        if (!isManaged) 
        {
            releaser.ManageLifetime(feature);
            isManaged = true;
        }

        // Do other stuff
    }
}
3
  • Actually I´m using Marshal.RelaseComObject. Maybe the ComReleaserClass manages things better. Will it determine that no more references exist for the COM-object and than release it? Or what happens when feature is null after last iteration? Jan 23 '17 at 15:36
  • ComReleaser just holds an array of references that are released when it is being disposed. To release the objects, ComReleaser calls Marshal.ReleaseComObject internally too. But the fact that ComReleaser is IDisposable makes it easier to use than inventing your own releasing scheme. Even if an exception is thrown, using will make sure the objects are released properly. Setting feature to null does not make a difference, the ComReleaser holds a reference to the instance of the feature, not to the variable you are using.
    – Berend
    Jan 24 '17 at 7:51
  • Ahh, that means feature references the same RCW as the ComReleaser-class does also, thus when feature == null there´s still one reference to the actual COM-object alive - that one within the ComReleaser which it can then easyly release. Thanks fro the explanation. Jan 24 '17 at 7:54
0

Another opportunity was given to me from a collegue and I want to share it with you. We can just create another reference to our COM-object within the loop and release that one when we´re gone:

IFeature tmp;
while((feature = cursor.NextFeature) != null)
{
    // do something with the feature
    tmp = feature;
}
Marshal.ReleaseComObject(tmp);

This works because tmp will allways point to the very last feature when iteration is finished. Thus it can easily be released.

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.