none
How do I force the Dispose method to be called automatically? RRS feed

  • Question

  • Hi,

    I'm writing a kind of library in managed code.

    P.e. a class with a window inside. When the class is constructed, the window is opened, and when Dispose is called, the window is closed again.

    Now I want to make sure the dispose method is ALWAYS called. But I can not force our users to call the Dispose method. What can I do to ensure the Dispose method is called automatically?

    Here are some ideas, but they did not work so far.

    * Force the creation of a class on stack. But there are no (safe?) methods to decide whether an object is created on stack or heap.

    * Add an ASSERT in the Finalizer, when the window is still open. But this solution is really unhandy for our users!

    Has anyone a better idea?
    Tuesday, January 20, 2009 8:35 AM

Answers

  • You cannot get Dispose() called automatically beyond the using statement.  Which is voluntary by the user.  You can slap them really hard by throwing an exception in the finalizer when you see the object wasn't disposed.  Crashing the finalizer thread is the .NET version of dropping the F-word.
    Hans Passant.
    Tuesday, January 20, 2009 12:26 PM
    Moderator

All replies

  • Hi,

    Call Dispose method in class finalizer. In this case it will be automatically called by GC.
    Example:
    http://msdn.microsoft.com/en-us/library/b1yfkh5e(VS.71).aspx


    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Tuesday, January 20, 2009 9:26 AM
  • You cannot get Dispose() called automatically beyond the using statement.  Which is voluntary by the user.  You can slap them really hard by throwing an exception in the finalizer when you see the object wasn't disposed.  Crashing the finalizer thread is the .NET version of dropping the F-word.
    Hans Passant.
    Tuesday, January 20, 2009 12:26 PM
    Moderator
  • nobugz said:

    You cannot get Dispose() called automatically beyond the using statement.  Which is voluntary by the user.  You can slap them really hard by throwing an exception in the finalizer when you see the object wasn't disposed.  Crashing the finalizer thread is the .NET version of dropping the F-word.


    Hans Passant.


    I meant finalizer is called automatically.

    Vitaliy Liptchinsky http://dotnetframeworkplanet.blogspot.com/
    Tuesday, January 20, 2009 1:25 PM
  • There's no way to automatically dispose something without a using statement.

    You might want to use the "Close-is-dispose" convention: i.e. Close and Dispose do the same thing. Many classes in the runtime do this, so there's no harm no foul.

    As to Disposing automatically after OnClose.... I'm not quite sure how you would do this in managed clases (native windows would destruct in response to a native WM_NCCLOSE message). Perhaps you could do

    Dispatcher.BeginInvoke((Action) delegate { Dispose(); },  ...) at lowest possible priority.

    A full-blown dispose of a window seems a bit draconian though. Perhaps better would be to use either an WM_NCCLOSE handler, or an OnClose handler in conjunction with the BeginInvoke@low-prioity to reclaim critical resources like window handles. A "semi-dispose", if you like.
    Thursday, January 22, 2009 1:49 PM