AnsweredAssumed Answered

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

Question asked by huangs3 on Aug 2, 2012
Latest reply on Aug 9, 2012 by Neil
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);
                    }
                }
            }
        }

Outcomes