List Info

Thread: partial calss and Dispose




partial calss and Dispose
user name
2006-08-01 11:23:57
I have a problem Disposing of objects.

1. Create a new UI component, XXX, in the form designer.
    I now have XXX.cs and XXX.Designer.cs

I also have, in XX.Designer.cs this override
         protected override void Dispose(bool disposing)
         {
             if (disposing && (components != null))
             {
                 components.Dispose();
             }
             base.Dispose(disposing);
         }

now in my XXX.cs I create an System.Drawing.Image and I load
it from a
file or whatever.

where do I dispose my image? It's not a Component so I
can't add it to
components. I could create a Component adapter that takes a
disposible
thing, but that seems a bit naff.

Ok maybe the Form.Closing event. well what if XXX inherits
UserControl
and not Form. Am I just supposed to hang off the form
closing event.

Both of these seem a big smelly/hackish

Please let me know if I'm being a nugget.

adam
partial calss and Dispose
user name
2006-08-01 11:45:19
This is a rather irritating feature of how VS splits things
up.

Apparently the official way to dispose of component-level
resources when
you're using the designer to design that component is to
add a handler
for your own Disposed event, and dispose of whatever needs
disposing in
there.

The Component class (base class of Form, UserControl, and
pretty much
all design surfaces) raises this event when the component is
disposed.

Barf. But there it is.


-- 
Ian Griffiths

-----Original Message-----
From: adam
Sent: 01 August 2006 12:24

I have a problem Disposing of objects.

1. Create a new UI component, XXX, in the form designer.
    I now have XXX.cs and XXX.Designer.cs

I also have, in XX.Designer.cs this override
         protected override void Dispose(bool disposing)
         {
             if (disposing && (components != null))
             {
                 components.Dispose();
             }
             base.Dispose(disposing);
         }

now in my XXX.cs I create an System.Drawing.Image and I load
it from a
file or whatever.

where do I dispose my image? It's not a Component so I
can't add it to
components. I could create a Component adapter that takes a
disposible
thing, but that seems a bit naff.

Ok maybe the Form.Closing event. well what if XXX inherits
UserControl
and not Form. Am I just supposed to hang off the form
closing event.

Both of these seem a big smelly/hackish

Please let me know if I'm being a nugget.

adam
partial calss and Dispose
user name
2006-08-01 12:10:59
I initially created and adapter [1] and added it to the
components
collection.

Is this what is recommended?
Image m_image;
public XXX()
{
   InitializeComponent();
   m_image = new Bitmap(10,10);
   Disposed += new EventHandler(XXX_Disposed);
}
void GalleryControl_Disposed(object sender, EventArgs e)
{
   Disposed -= new EventHandler(XXX_Disposed);
   if (m_image != null)
     m_image.Dispose();
}

unless there is something wrong with the adapter (or
whatever pattern it
is) I prefer the adapter. The ComponentAdapter c'tor could
be changed to
take a params IDisposeable[] items if you need to dispose of
lots of things.

Thinking about it though there might be other things you
need to know
and only XXX instance objects know that so maybe the
eventing is better.

adam

Ian Griffiths wrote:
> This is a rather irritating feature of how VS splits
things up.
>
> Apparently the official way to dispose of
component-level resources when
> you're using the designer to design that component is
to add a handler
> for your own Disposed event, and dispose of whatever
needs disposing in
> there.
>
> The Component class (base class of Form, UserControl,
and pretty much
> all design surfaces) raises this event when the
component is disposed.

[1]
public class ComponentAdapter : Component
{
   IDisposable m_item;
   public ComponentAdapter(IDisposable item)
   {
     m_item = item;
   }
   protected override void Dispose(bool disposing)
   {
       if (disposing)
         m_item.Dispose();
       base.Dispose(disposing);
   }
}
partial calss and Dispose
user name
2006-08-01 12:20:43
Usually I move the Dispose method from XXX.Designer.cs to
XXX.cs. The
designer won't touch Dispose once the files are created, so
nothing's
broken.

Regards,
Thomas

On 8/1/06, adam <adam.maillist01hotpop.com> wrote:
> I have a problem Disposing of objects.
>
> 1. Create a new UI component, XXX, in the form
designer.
>     I now have XXX.cs and XXX.Designer.cs
>
> I also have, in XX.Designer.cs this override
>          protected override void Dispose(bool
disposing)
>          {
>              if (disposing && (components !=
null))
>              {
>                  components.Dispose();
>              }
>              base.Dispose(disposing);
>          }
>
> now in my XXX.cs I create an System.Drawing.Image and I
load it from a
> file or whatever.
>
> where do I dispose my image? It's not a Component so I
can't add it to
> components. I could create a Component adapter that
takes a disposible
> thing, but that seems a bit naff.
>
> Ok maybe the Form.Closing event. well what if XXX
inherits UserControl
> and not Form. Am I just supposed to hang off the form
closing event.
>
> Both of these seem a big smelly/hackish
>
> Please let me know if I'm being a nugget.
>
> adam
>
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )