tags:

views:

61

answers:

2

hello,

i have a view model that is used as the data source for my custom control. in the view model's constructor i set up a WMI ManagementEventWatcher and start it. my view model implements IDisposable, so i stop the watcher in the Dispose method.

when i embed the custom control into a window, and then close the window to exit the application it throws an InvalidComObjectException saying "COM object that has been separated from its underlying RCW cannot be used". this happens because of my watcher, and if i do not create it, there is no exception. there is no additional information about the exception such as stack trace, etc.

my guess is that something keeps the view model until the thread that the watcher uses terminates but before the watcher is stopped. and i do not know how to handle this.

any advice? thanks konstantin


public abstract class ViewModelBase : IDisposable, ...
{
    ...

    protected virtual void OnDispose() { }

    void IDisposable.Dispose()
    {
        this.OnDispose();
    }
}

public class DirectorySelector : ViewModelBase
{
    private ManagementEventWatcher watcher;

    private void OnWMIEvent(object sender, EventArrivedEventArgs e)
    {
        ...
    }

    protected override void OnDispose()
    {
        if (this.watcher != null)
        {
            this.watcher.Stop();
            this.watcher = null;
        }
        base.OnDispose();
    }

    public DirectorySelector()
    {
        try
        {
            this.watcher = new ManagementEventWatcher(new WqlEventQuery(...));

            this.watcher.EventArrived += new EventArrivedEventHandler(this.OnWMIEvent);
            this.watcher.Start();
        }
        catch (ManagementException)
        {
            this.watcher = null;
        }
    }
}
A: 

The reason is that your object gets doubly disposed. How have you implemented your disposal? Put the dispose and finalize section of your code in here.

UPDATE Your disposal pattern is incorrect and also I cannot see where Dispose is called - there is no mention of that even if you call it.

Disposal pattern must be done with a finalizer. This post is a good one and has a correct disposal pattern:

http://stackoverflow.com/questions/1492879/com-object-that-has-been-separated-from-its-underlying-rcw-can-not-be-used-why

UPDATE 2

It lacks the finalizer:

    ~ActiveDirectoryObject()
    {
        Dispose(false);
    }
Aliostad
i have updated the question
akonsu
The exception should happen on the call to this.watcher.Stop(), means like Aliostad said it's already gone to bye bye land (RCW count decremented to zero).
MrDosu
I have updated the answer.
Aliostad
thanks. a breakpoint at watcher.Stop() never gets hit, so i do not see how an incorrect implementation of the disposal pattern could be the reason... if i call Dispose on my view model manually from inside my main window's OnClosed() method, the exception does not get thrown.
akonsu
would you please explain why do you think that i need a finalizer? the post that you are referring to does not have one. but a i said above, this is not the cause. it fails before IDisposable.Dispose is invoked.
akonsu
Exactly! You need to call Dispose() on the disposable object you have and if you implement dispoable, again you need to call it. "if i call Dispose on my view model manually from inside my main window's OnClosed() method, the exception does not get thrown" well this is what you need to do! Client is responsible for calling dispose. Also you are right, that post does not have a finalizer and it should have, I have updated my answer.
Aliostad
@Aliostad: thanks for your help. i was assuming that IDisposable gets called by the framework automatically, but it does not. so no matter how closely the disposable pattern is followed, it does not help unless Dispose is called explicitly. i have added my own answer that refers to an article that in turn refers to a site explaining that.
akonsu
Framework will call for you if you do a finalizer.
Aliostad
i tried that, and it did not call it.
akonsu
No, it won't because the process is exiting. That is useful for GC to call dispose for you. If you are closing, then you have to call Dispose manually.
Aliostad
A: 

this article has the solution: http://stackoverflow.com/questions/502761/disposing-wpf-user-controls

basically, WPF dos not seem to use IDisposable anywhere, so the app needs to cleanup itself explicitly. so in my case, i subscribe to the Dispatcher.ShutdownStarted event from my control that uses the view model that needs to be disposed, and dispose the control's DataContext from the event handler.

akonsu