Page 1 of 1

COM server won't let go of reference

Posted: Thu Jun 21, 2007 7:49 am
by darchangel
In scripting, when doing this to get a reference to: MediaMonkey

Code: Select all

SongsDB.SDBApplicationClass SDB = new SongsDB.SDBApplicationClass();
you can never undo it. Regardless of which application is shut down first, MediaMonkey throws a COM Server Warning. There's no way to detach, even after the calling app is shut down.

Posted: Thu Jun 21, 2007 9:42 am
by Steegy
This might not be a MM specific problem.
However, is it really necessary that MediaMonkey shows the "COM Server Warning"? Can't it just close?

Posted: Thu Jun 21, 2007 10:21 am
by darchangel
Steegy wrote:is it really necessary that MediaMonkey shows the "COM Server Warning"? Can't it just close?
I definitely like that idea best. However, barring that, there still needs to be some way to code your way out of this mess.

Posted: Thu Jun 21, 2007 11:43 am
by Peke
Like I said I'm not a .net coder But when I wanna Ensure that No COM Connection is left I do this in Delphi;

Code: Select all

SDB := CreateOleObject('SongsDB.SDBApplication');
// Free variable to avoid COM Warning
SDB := Nil;

Posted: Thu Jun 21, 2007 12:12 pm
by Steegy
:lol:
I tried SDB = null; a thousand times, without luck (if only it was that easy ;)). Maybe by using Late Binding, we can get rid of the warning. The Early Binding uses RCW (Runtime Callable Wrapper) and GC (Carbage Collection) and stuff like that, so it's very very difficult (or impossible) to control things yourself.
.NET makes a lot of things easy, except if really need to use COM, Shell, Kernel, Plugins, ... (the not-so-GUI stuff)

Posted: Thu Jun 21, 2007 12:12 pm
by Abel
I don't know about the VB scripts, but in C++ if I have a .NET reference that I explicitly want to clean up I can do an explicit delete, even though it's a managed pointer.

I seem to remember that the explicit NOTHING assignment was the alternative solution for VBA in Excel, so you may indeed want to try that.

Abel

Posted: Thu Jun 21, 2007 12:17 pm
by Abel
Steegy wrote::lol:
I tried SDB = null; a thousand times, without luck (if only it was that easy ;)).
Have you tried SDB = nothing; instead?

Abel

Posted: Thu Jun 21, 2007 12:21 pm
by Peke
Is there Any way to free (iDispatch) in .NET. I seriously doubt that .NET missed something like that.

One small Q? Is there way to Kill var in .net and as soon as You kill it MM closes? (Just trying to figure this out, was reading .NET Interoperability: COM Interop and you all made me interested in this)

Posted: Thu Jun 21, 2007 12:49 pm
by Steegy
Just a desperate try:

Code: Select all

private void ScreenSaverForm_FormClosed(object sender, FormClosedEventArgs e)
{
    try
    {
        Marshal.FinalReleaseComObject(SDB);  // Don't (shouldn't be necessary)
        SDB = null;

        GC.Collect();  // Also not recommended
        GC.WaitForPendingFinalizers();  // Yeah, same as before.

        Process.GetCurrentProcess().Kill();   // Don't do this!!!
    }
    catch { }
}
Remark about not doing an own process kill: Why not? It's the easiest way to quickly get rid of the program and free all memory (at least, as long as you don't use not-GC-able objects). I do it a lot with MediaMonkey and Maxthon2 too.

Posted: Thu Jun 21, 2007 3:38 pm
by darchangel
The longer I try to make sense of it the less sense it makes.

If I create a new app, create a reference to MM and set the reference to null when I shutdown, I get this COM warning. If I add a button to the form, the COM warning goes away. If I reference any embedded resource, I get the COM warning. I've also found another dozen idiosyncrasies that make no sense. I've invested a lot of time into making this because when I began I thought MM was good for development. Now I'm ready to give up on it altogether. The experience has been entirely infuriating.

I hope this at least works for all the VBScript-ers out there because it is a nightmare in .NET