[IronPython] COM Interop / Dispose wierdness

Dan Shechter dans at houmus.org
Thu Apr 20 17:59:07 CEST 2006


OK, I pretty much get what you're aiming for.
You are saying that since there is no deterministic order for Finalization
the COM object (which implements IWMMetaDataEditor) may be disposed of
BEFORE my "Wrapper" Finalizer get's called.

But shouldn't the order be enforced by someone (CLR that is) so that AT
LEAST the managed components get finalized and destroyed BEFORE the
unmanaged ones?

I seem to remember there should be some thing like this...

I'll dig deeper and also post in the WMF forums,
Thanks,
	Shechter.

-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Dino Viehland
Sent: Monday, April 17, 2006 18:24
To: Discussion of IronPython
Subject: Re: [IronPython] COM Interop / Dispose wierdness

This could be an order of finalization issue - if you let things shut down
entirely then all the finalizers in the system would be eligible to be run
at once.  But if you dispose of the MetadataEditor by hand anything it
depends upon won't have its finalizer run yet.

At shutdown time the CLR will run all remaining finalizers and we could be
cleaning up in the "wrong" order (although there's really no right order,
finalization is non-deterministic).

I think you'd need to talk to the WMF SDK team about the issue as they'll
understand better their finalization issues (or wrap dispose in a try/catch
InvalidCastException block :) ).


Do you want to help develop Dynamic languages on CLR?
(http://members.microsoft.com/careers/search/details.aspx?JobID=6D4754DE-11F
0-45DF-8B78-DC1B43134038)

-----Original Message-----
From: users-bounces at lists.ironpython.com
[mailto:users-bounces at lists.ironpython.com] On Behalf Of Dan Shechter
Sent: Monday, April 17, 2006 3:38 AM
To: 'Discussion of IronPython'
Subject: [IronPython] COM Interop / Dispose wierdness

Hi,
I have a c# class which is a wrapper around the WMF (Windows Media Format)
SDK.
One of the classes, which wraps the IWMMetadataEditor interface roughly
looks like this:
namespace WMFSDKWrapper
{
    public class MetadataEditor : IDisposable, IEnumerable
    {
          bool isDisposed = false;
          private IWMMetadataEditor editor;
          ...

        public void Flush()
        { editor.Flush(); }

        private void Dispose(bool disposing)
        {
            if (!isDisposed)
            {
                if (disposing)
                        ; // No managed resources to clear up...
                // Clear up unmanaged resources
                Flush();
            }
            isDisposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~MetadataEditor()
        { Dispose(false); }
    }
}

Now, the wrapper (which obviously has more code) works perfectly.
I can change a WM file's metadata or query it as I wish.
I can call the Flush(), Close() or even Dispose() method directly and they
all work fine...
i.e.: If I call the Dispose() method "manually" I get:
IronPython 1.0.2281 (Beta) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import wmf
>>> m = wmf.MetadataEditor("a.wma")
>>> m[wmf.MediaMetadata.Title]
'123'
>>> m.Dispose()
>>> ^Z
(IronPythonConsole.exe exits cleanly)

The problem I'm getting is that if exit the IronPython console without
calling the object's Dispose method beforehand I get
Something like this, regardless of what I did with the COM object (i.e.,
read-only or read-write):
IronPython 1.0.2281 (Beta) on .NET 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
>>> import wmf
>>> m = wmf.MetadataEditor("a.wma")
>>> m[wmf.MediaMetadata.Title]
'123'
>>> ^Z
Unhandled exception:
Traceback (most recent call last):
  File WMFSDKWrapper, line unknown, in Finalize
  File WMFSDKWrapper, line unknown, in Dispose
  File WMFSDKWrapper, line unknown, in Flush
  File WMFSDKWrapper, line unknown, in Flush
TypeError: Unable to cast COM object of type 'System.__ComObject' to
interface type 'WMFSDKWrapper.IWMMetadataEditor'. T
his operation failed because the QueryInterface call on the COM component
for the interface with IID '{96406BD9-2B2B-11D
3-B36B-00C04F6108FF}' failed due to the following error: No such interface
supported (Exception from HRESULT: 0x80004002
 (E_NOINTERFACE)).

Unhandled Exception: System.InvalidCastException: Unable to cast COM object
of type 'System.__ComObject' to interface ty
pe 'WMFSDKWrapper.IWMMetadataEditor'. This operation failed because the
QueryInterface call on the COM component for the
 interface with IID '{96406BD9-2B2B-11D3-B36B-00C04F6108FF}' failed due to
the following error: No such interface suppor
ted (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
   at WMFSDKWrapper.IWMMetadataEditor.Flush()
   at WMFSDKWrapper.MetadataEditor.Flush()
   at WMFSDKWrapper.MetadataEditor.Dispose(Boolean disposing)
   at WMFSDKWrapper.MetadataEditor.Finalize()

        Any ideas?
        Shechter.


_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
users mailing list
users at lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com





More information about the Ironpython-users mailing list