views:

185

answers:

1

Hello.

I'm using a third party COM component by means of a .NET interop assembly in IronPython, as seen here: http://stackoverflow.com/questions/1237200/how-can-i-use-a-vb6-com-reference-in-ironpython

My experience in this area has been great, I'm very impressed by the amount of stuff that works seamlessly... except one thing.

The 3rd party COM component uses Microsoft's DAO library (not sure what version) to work with a database created by yet another 3rd party application. The problem is that this database file gets recreated during my program's runtime, and I'd like to 'de-initialize' this COM object. In particular, running my code (which accesses the COM component's attributes, calls it's methods, etc) works fine the first time I run my routine. The next time, however, the third party COM component displays a messagebox saying the database (MSAccess 95 .mdb file) is already in use, and offers an option to retry. Pressing retry works reliably, so the main issue is that this dialog comes up.

So, my theory is the COM component is leaking handles to the db, and I can't find a 'cleanup' method to call. I've tried .Dispose(), but that has not worked.

My last resort is making the code that calls to the COM object a separate IronPython process that interacts with my main process via std in/out as the role the COM object serves is more of a 'give me all this information right now' use case instead of a continually required dependency.

I am hoping to avoid that scenario, and since i'm not to familiar with COM (or really, .NET for that matter) I can only hope i'm missing an obvious .Dispose method or the like.

If there's no clean way, can I forcibly unload the assembly from my process, analogous to repeated FreeLibrary calls in native code? (I guarantee that my code won't be using the object anymore, so I shouldn't need to worry about missing refs on my part leading to a memory protection error)


EDIT:

I wasn't able to solve this, so I went the out of process method and let windows clean up when my child batch process shuts down.

+1  A: 

Not sure about IronPython, but when working with COM in C#, you need to call Marshal.ReleaseComObject after use to ensure the reference count is properly decremented.

Otherwise you will leak...Dispose() does not do this for you.

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.releasecomobject.aspx

FlySwat
I've read up on ReleaseComObject and I now know more about it. Unfortunately, my 3rd party COM component isn't behaving properly. I checked my code for any references, and they're all cleaned up. In particular, I'm now using the ComWrap class here http://dump.cbwhiz.com/comwrap.py to auto-release references.
CB

related questions