views:

140

answers:

3

I asked a question earlier today, but I think I need to approach it in a different way (on top of that there was a "hang up" in regards to DataSet).

Here's a class that encapsulates the creation of a Font (in other words, it is reading data from an xml file and is creating a font, at runtime, based on what it reads from that file):

public class FontCreator
{
    private Font m_TheFont = null;

    public FontCreator( ... some parameters ... )
    {
        m_TheFont = GetTheFont();
    }

    public Font TheFont
    {
        return m_TheFont;
    }

    private Font GetTheFont()
    {
        // code, and more code, that eventually leads to:

        Font f = new Font(fntFamily, fntSize, fntStyle);
        return f;
    }
}

The consumer of the FontCreator class looks something like:

public class TheConsumer()
{
    private FontCreator m_FontCreator = null;

    public TheConsumer()
    {
        m_FontCreator = new m_FontCreator( ... some parameters ... );
        Initialize();
    }

    private void Initialize()
    {
        InitializeThis();
        InitializeThat();
    }

    private void InitializeThis()
    {
        .... some code ...
        SomeObject.ApplyFont(m_FontCreator.TheFont);
    }

    private void InitializeThat()
    {
        ... some code ...
        SomeObject.ApplyFont(m_FontCreator.TheFont);
    }
}

What code do you add, and where, to ensure that "TheFont"'s Dispose method is explicitly called?

+3  A: 
public TheConsumer()
{
    using (m_FontCreator = new m_FontCreator( ... some parameters ... ))
    {
        Initialize();
    }
}
Neil T.
So what you are saying is that I should modify the "FontCreator" class to be IDisposable? Because your code won't compile unless I do that. If that's the case (make FontCreator IDisposable), can I see how you would do that? Thx.
JustLooking
FontCreator doesn't implement IDisposable...
Meta-Knight
you made a typo- it should be new FontCreater :)
Stan R.
public class FontCreator : IDisposable{ ...}Stan: The spelling is correct. :)
Neil T.
@Neil. using (m_FontCreator = new m_FontCreator( ... some parameters ... )) ..is incorrect :P
Stan R.
My mistake, Stan...I thought you were referring to the spelling of "Creator". Yes, I should have new'ed the class, not the instance. Bumping your earlier comment up as a self-imposed penalty... :)
Neil T.
+3  A: 

If you don't wish to maintain a reference to TheFont after it is initially used, then call it's Dispose method in your constructor, right after Initialize. If you wish to keep TheConsumer alive for a while and maintain a reference to TheFont, it gets more interesting. Two Options:

  1. You can have TheFont's dispose method called from the Destructor of the TheConsumer object. This is not the common practice and has problems. Mainly, this is not called until garbage collection happens. Better is:
  2. You can make the TheConsumer object itself implement IDisposable, and call TheFont.Dispose from TheConsumer.Dispose. Since TheConsumer implements IDisposable, the code that uses it should call its Dispose method.

Edit in response to harsh comment! Yes, I should have made clear to only use 1 in addition to 2, if at all. I know all developers everywhere are supposed to notice when IDisposable is implemented, but they often don't. If the referenced managed resource might really remain around a long time and cause problems if not properly disposed, I sometimes have a safety Dispose() method call in the destructor of the object holding the reference. Is that so wrong? :)

Patrick Karcher
Henk could you elaborate?
JustLooking
@Henk: One more follow-up: Let's say I implement IDisposable on FontCreator, but I have no Finalizer. In my Dispose function I call: m_TheFont.Dispose(). Now, someone comes along, they use FontCreator, but don't use a "using" or call Dispose on the "TheFont" getter. In addition, they don't call Dispose on FontCreator. Therefore, m_TheFont.Dispose() is never called. Didn't we just leak the unmanaged memory that m_TheFont was using? Even though we are one level removed?
JustLooking
@Henk: Dispose() is not called by GC, right? Now, making the GC deal with a finalizer method has overhead (and can make object stay around longer), but might it not be worth it to make sure an important Dispose method gets called? To me it seems like a judgement call.Let's say an object BigShot is long-lived anyway, and has referenced to many LittleShot (implementing IDisposable) objects whose unmanaged resources are known to really need clean-up. Shouldn't we weigh creating a BigShot finalizer against making sure all those resources are cleaned up? It seems like a judgement call to me.
Patrick Karcher
+3  A: 

I am confused, if you want to quickly use the font creater object then implement IDisposable on the FontCreater and use

using(m_FontCreator = new FontCreater(....))
{
   InitializeThis();
   InitializeThat();
}

If you need to keep the instance of the FontCreater through the lifetime of TheConsumer, then implement IDisposable on both FontCreater and TheConsumer classes.

public class TheConsumer : IDisposable
{
  void Dispose()
  {
     if(m_FontCreator != null)
          m_FontCreator.Dispose();
  }
}

then use TheConsumer class like so

using(TheConsumer consumer = new TheConsumer(....))
{
  ....
}
Stan R.