Select to view content in your preferred language

Any way to check whether a COM ArcObjects object is already released?

1891
12
Jump to solution
08-02-2012 10:02 AM
SuiHuang
Frequent Contributor
Hi Everybody:

    I found that after an ArcObject COM object is release, some operations such as checking interface cannot be performed on it and otherwise it will throw exception.
    Is there any way for me to detect whether the object has already been release? I am trying not to use the TRY-CATCH pair to handle this logic...
    Thank you.


**************** Released twice by my utility function ******
    IPoint obj = new PointClass();
    ReleaseObjects(obj);
    ReleaseObjects(obj);


**************** Utility functions *********
        public void ReleaseObjects(params object[] comObjList)
        {
            foreach (object obj in comObjList)
            {
                if (obj != null)
                {
                    if (obj is System.Collections.IEnumerable)
                    {
                        ReleaseComs(obj as IEnumerable);
                    }
                    else
                    {
                        Marshal.ReleaseComObject(obj);
                    }
                }
            }
        }

        private void ReleaseComs(IEnumerable Objects)
        {
            foreach (object obj in Objects)
            {
                if (obj != null)
                {
                    if (obj is IEnumerable)
                    {
                        ReleaseComs(obj as IEnumerable);
                    }
                    else
                    {
                        Marshal.ReleaseComObject(obj);
                    }
                }
            }
        }
0 Kudos
12 Replies
LeoDonahue
Deactivated User
For some reason, your statement is leading me to believe that you are releasing an object, then trying to do a method call on that object, but first checking whether the object you released is null, otherwise calling that method on a released object without first re-assigning that released object the value of null is giving you the exception.  What do you expect?  If you were in Java, assigning an object reference to null would still give you an exception when you tried calling a method on that object.

I still don't understand why you are trying to call a method on an object after you've released it.  Can anyone explain that to me?  I would really like to understand why you are trying to do this.
0 Kudos
SuiHuang
Frequent Contributor
For some reason, your statement is leading me to believe that you are releasing an object, then trying to do a method call on that object, but first checking whether the object you released is null, otherwise calling that method on a released object without first re-assigning that released object the value of null is giving you the exception.  What do you expect?  If you were in Java, assigning an object reference to null would still give you an exception when you tried calling a method on that object.

I still don't understand why you are trying to call a method on an object after you've released it.  Can anyone explain that to me?  I would really like to understand why you are trying to do this.


The exception is not caused by calling a method of that released COM object, but caused by releasing it again with my generic releasing method... it use "if (obj is IEnumerable)" to check the interface it supports. Because it is already released, the checking will fail in the second call of the releasing method.

Now I decide to resolve this issue by coding carefully, not to release the same COM object twice, even though the logic will be a bit more comoplicated.
0 Kudos
NeilClemmons
Honored Contributor
The exception is not caused by calling a method of that released COM object, but caused by releasing it again with my generic releasing method... it use "if (obj is IEnumerable)" to check the interface it supports. Because it is already released, the checking will fail in the second call of the releasing method.

Now I decide to resolve this issue by coding carefully, not to release the same COM object twice, even though the logic will be a bit more comoplicated.


Set the object to null after the call to ReleaseComObject.  After the object is released, it's value isn't null so if you call the method again it will try to release it again.  If you set the object to null after releasing it then the next time you call the method it will see the object is null and not try to release it a second time.

Also, as you said, more careful programming is what you really need.  You should be releasing objects when you're done with them.  This is usually right before they go out of scope so in reality there shouldn't be another place in the code where you would even have access to the object in order to try to release it.
0 Kudos