List Info

Thread: Unmanaged resources in a static class




Unmanaged resources in a static class
country flaguser name
United States
2008-09-17 20:25:25
I have a static class that holds quite a few unmanaged
resources (it
basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
that are likely to be used at regular intervals by the
application). Being
concerned about resource cleanup, I naturally (  ) thought
about their
disposal. However, static classes do not allow finalizers to
be defined.

In one sense that makes sense - the reason for having a
static class that
manages these resources is that they are likely to be
required repeatedly
right up until the app shuts down. However I am a bit
nervous - this means
I'm entirely dependant on Windows to release all these
resources when the
process ends, and although Windows should do that, to be
honest I'm not
100% sure I trust it to do so, I'd feel more comfortable if
my managed
code contained something to explicitly ensure these
resources are
released. (I trust .NET rather more than I trust the Windows
OS  )

Any thoughts? Am I being too cautious when I should just
trust Windows to
handle resource release, or is there some other way to deal
with this
situation?

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
country flaguser name
United States
2008-09-17 20:46:35
Your type is destroyed when the AppDomain goes away...can
you wrap it in a
AppDomain and control that lifespan?

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Simon
Robinson
Sent: Wednesday, September 17, 2008 9:25 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Unmanaged resources in a static
class

I have a static class that holds quite a few unmanaged
resources (it
basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
that are likely to be used at regular intervals by the
application). Being
concerned about resource cleanup, I naturally (  ) thought
about their
disposal. However, static classes do not allow finalizers to
be defined.

In one sense that makes sense - the reason for having a
static class that
manages these resources is that they are likely to be
required repeatedly
right up until the app shuts down. However I am a bit
nervous - this means
I'm entirely dependant on Windows to release all these
resources when the
process ends, and although Windows should do that, to be
honest I'm not
100% sure I trust it to do so, I'd feel more comfortable if
my managed
code contained something to explicitly ensure these
resources are
released. (I trust .NET rather more than I trust the Windows
OS  )

Any thoughts? Am I being too cautious when I should just
trust Windows to
handle resource release, or is there some other way to deal
with this
situation?

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
country flaguser name
United States
2008-09-17 20:46:35
Your type is destroyed when the AppDomain goes away...can
you wrap it in a
AppDomain and control that lifespan?

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Simon
Robinson
Sent: Wednesday, September 17, 2008 9:25 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: [ADVANCED-DOTNET] Unmanaged resources in a static
class

I have a static class that holds quite a few unmanaged
resources (it
basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
that are likely to be used at regular intervals by the
application). Being
concerned about resource cleanup, I naturally (  ) thought
about their
disposal. However, static classes do not allow finalizers to
be defined.

In one sense that makes sense - the reason for having a
static class that
manages these resources is that they are likely to be
required repeatedly
right up until the app shuts down. However I am a bit
nervous - this means
I'm entirely dependant on Windows to release all these
resources when the
process ends, and although Windows should do that, to be
honest I'm not
100% sure I trust it to do so, I'd feel more comfortable if
my managed
code contained something to explicitly ensure these
resources are
released. (I trust .NET rather more than I trust the Windows
OS  )

Any thoughts? Am I being too cautious when I should just
trust Windows to
handle resource release, or is there some other way to deal
with this
situation?

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
user name
2008-09-17 21:22:48
I think you should wrap those handles in an existing or new
SafeHandle class
and as Shawn pointed out, when the AppDomain goes away, the
SafeHandle
wrappers will be finalized and your resources released.

Sébastien
On Wed, Sep 17, 2008 at 9:25 PM, Simon Robinson
<simonsimonrobinson.com>wrote:

> I have a static class that holds quite a few unmanaged
resources (it
> basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
> that are likely to be used at regular intervals by the
application). Being
> concerned about resource cleanup, I naturally (  ) thought
about their
> disposal. However, static classes do not allow
finalizers to be defined.
>
> In one sense that makes sense - the reason for having a
static class that
> manages these resources is that they are likely to be
required repeatedly
> right up until the app shuts down. However I am a bit
nervous - this means
> I'm entirely dependant on Windows to release all these
resources when the
> process ends, and although Windows should do that, to
be honest I'm not
> 100% sure I trust it to do so, I'd feel more
comfortable if my managed
> code contained something to explicitly ensure these
resources are
> released. (I trust .NET rather more than I trust the
Windows OS  )
>
> Any thoughts? Am I being too cautious when I should
just trust Windows to
> handle resource release, or is there some other way to
deal with this
> situation?
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
user name
2008-09-17 21:33:14
why not keep a static reference to an instance of the class
instead of
using a static class?

Cheers,

Greg
On 9/17/08, Simon Robinson <simonsimonrobinson.com>
wrote:
> I have a static class that holds quite a few unmanaged
resources (it
> basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
> that are likely to be used at regular intervals by the
application). Being
> concerned about resource cleanup, I naturally (  ) thought
about their
> disposal. However, static classes do not allow
finalizers to be defined.
>
> In one sense that makes sense - the reason for having a
static class that
> manages these resources is that they are likely to be
required repeatedly
> right up until the app shuts down. However I am a bit
nervous - this means
> I'm entirely dependant on Windows to release all these
resources when the
> process ends, and although Windows should do that, to
be honest I'm not
> 100% sure I trust it to do so, I'd feel more
comfortable if my managed
> code contained something to explicitly ensure these
resources are
> released. (I trust .NET rather more than I trust the
Windows OS  )
>
> Any thoughts? Am I being too cautious when I should
just trust Windows to
> handle resource release, or is there some other way to
deal with this
> situation?
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com

>


--
It is the mark of an educated mind to be able to entertain a
thought
without accepting it.

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
country flaguser name
United States
2008-09-17 21:41:44
That's a lot less trouble than my solution...I take back
most of what I've
said about you Greg ;)

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Greg
Young
Sent: Wednesday, September 17, 2008 10:33 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Unmanaged resources in a
static class

why not keep a static reference to an instance of the class
instead of
using a static class?

Cheers,

Greg
On 9/17/08, Simon Robinson <simonsimonrobinson.com>
wrote:
> I have a static class that holds quite a few unmanaged
resources (it
> basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
> that are likely to be used at regular intervals by the
application). Being
> concerned about resource cleanup, I naturally (  ) thought
about their
> disposal. However, static classes do not allow
finalizers to be defined.
>
> In one sense that makes sense - the reason for having a
static class that
> manages these resources is that they are likely to be
required repeatedly
> right up until the app shuts down. However I am a bit
nervous - this means
> I'm entirely dependant on Windows to release all these
resources when the
> process ends, and although Windows should do that, to
be honest I'm not
> 100% sure I trust it to do so, I'd feel more
comfortable if my managed
> code contained something to explicitly ensure these
resources are
> released. (I trust .NET rather more than I trust the
Windows OS  )
>
> Any thoughts? Am I being too cautious when I should
just trust Windows to
> handle resource release, or is there some other way to
deal with this
> situation?
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
http://discuss.develop.com

>


--
It is the mark of an educated mind to be able to entertain a
thought
without accepting it.

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
user name
2008-09-17 21:22:48
I think you should wrap those handles in an existing or new
SafeHandle class
and as Shawn pointed out, when the AppDomain goes away, the
SafeHandle
wrappers will be finalized and your resources released.

Sébastien
On Wed, Sep 17, 2008 at 9:25 PM, Simon Robinson
<simonsimonrobinson.com>wrote:

> I have a static class that holds quite a few unmanaged
resources (it
> basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
> that are likely to be used at regular intervals by the
application). Being
> concerned about resource cleanup, I naturally (  ) thought
about their
> disposal. However, static classes do not allow
finalizers to be defined.
>
> In one sense that makes sense - the reason for having a
static class that
> manages these resources is that they are likely to be
required repeatedly
> right up until the app shuts down. However I am a bit
nervous - this means
> I'm entirely dependant on Windows to release all these
resources when the
> process ends, and although Windows should do that, to
be honest I'm not
> 100% sure I trust it to do so, I'd feel more
comfortable if my managed
> code contained something to explicitly ensure these
resources are
> released. (I trust .NET rather more than I trust the
Windows OS  )
>
> Any thoughts? Am I being too cautious when I should
just trust Windows to
> handle resource release, or is there some other way to
deal with this
> situation?
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
user name
2008-09-17 21:33:14
why not keep a static reference to an instance of the class
instead of
using a static class?

Cheers,

Greg
On 9/17/08, Simon Robinson <simonsimonrobinson.com>
wrote:
> I have a static class that holds quite a few unmanaged
resources (it
> basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
> that are likely to be used at regular intervals by the
application). Being
> concerned about resource cleanup, I naturally (  ) thought
about their
> disposal. However, static classes do not allow
finalizers to be defined.
>
> In one sense that makes sense - the reason for having a
static class that
> manages these resources is that they are likely to be
required repeatedly
> right up until the app shuts down. However I am a bit
nervous - this means
> I'm entirely dependant on Windows to release all these
resources when the
> process ends, and although Windows should do that, to
be honest I'm not
> 100% sure I trust it to do so, I'd feel more
comfortable if my managed
> code contained something to explicitly ensure these
resources are
> released. (I trust .NET rather more than I trust the
Windows OS  )
>
> Any thoughts? Am I being too cautious when I should
just trust Windows to
> handle resource release, or is there some other way to
deal with this
> situation?
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at http://discuss.develop.com

>


--
It is the mark of an educated mind to be able to entertain a
thought
without accepting it.

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
country flaguser name
United States
2008-09-17 21:41:44
That's a lot less trouble than my solution...I take back
most of what I've
said about you Greg ;)

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:ADVANCED-DOTNETDISCUSS.DEVELOP.COM] On Behalf Of Greg
Young
Sent: Wednesday, September 17, 2008 10:33 PM
To: ADVANCED-DOTNETDISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Unmanaged resources in a
static class

why not keep a static reference to an instance of the class
instead of
using a static class?

Cheers,

Greg
On 9/17/08, Simon Robinson <simonsimonrobinson.com>
wrote:
> I have a static class that holds quite a few unmanaged
resources (it
> basically loads/creates a large number of GDI+ bitmaps,
brushes and pens
> that are likely to be used at regular intervals by the
application). Being
> concerned about resource cleanup, I naturally (  ) thought
about their
> disposal. However, static classes do not allow
finalizers to be defined.
>
> In one sense that makes sense - the reason for having a
static class that
> manages these resources is that they are likely to be
required repeatedly
> right up until the app shuts down. However I am a bit
nervous - this means
> I'm entirely dependant on Windows to release all these
resources when the
> process ends, and although Windows should do that, to
be honest I'm not
> 100% sure I trust it to do so, I'd feel more
comfortable if my managed
> code contained something to explicitly ensure these
resources are
> released. (I trust .NET rather more than I trust the
Windows OS  )
>
> Any thoughts? Am I being too cautious when I should
just trust Windows to
> handle resource release, or is there some other way to
deal with this
> situation?
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
http://discuss.develop.com

>


--
It is the mark of an educated mind to be able to entertain a
thought
without accepting it.

===================================
This list is hosted by DevelopMentorR  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


Re: Unmanaged resources in a static class
user name
2008-09-18 03:05:58
Hi,
As Greg says, make your static class an instance class (with
instance
methods, properties and resources, and your cleanup code in
its finaliser)
and then maintain one (and only one) static reference to an
instance of this
class.

One nice way of doing this is the Singleton pattern:
    http://www.dofactory.com/Patterns/PatternSingleton.aspx

Guaranteeing the thread-safety of a singleton is trickier
than you may
expect though:
    ht
tp://www.yoda.arachsys.com/csharp/singleton.html

Here's a thread-safe singleton wrapper I've used in the
past.  You can use
this sort of thing to make any instance class which has a
parameterless
constructor into a singleton:

namespace SomeNamespace
{
    public class Singleton<T> where T : new ()
    {
        public static T Instance
        {
            get
            {
                return Nested.Instance;
            }
        }

        private class Nested
        {
            public static readonly T Instance;

            static Nested ()
            {
                Instance = new T ();

                return;
            }
        }
    }
}

So if your new instance class is called
'MyUnmanagedHandler', you would just
access it in your code as:

    Singleton<MyUnmanagedHandler>.Instance

without any need for initialisation or setup at any specific
point in your
code.

And then, when the AppDomain is torn down, the finaliser of
your
MyUnmanagedHandler class runs and cleans up the unmanaged
resources you're
worried about.

Hope this helps,

    Geoff

On Thu, Sep 18, 2008 at 3:33 AM, Greg Young
<gregoryyoung1gmail.com> wrote:

> why not keep a static reference to an instance of the
class instead of
> using a static class?
>
> Cheers,
>
> Greg
> On 9/17/08, Simon Robinson <simonsimonrobinson.com> wrote:
> > I have a static class that holds quite a few
unmanaged resources (it
> > basically loads/creates a large number of GDI+
bitmaps, brushes and pens
> > that are likely to be used at regular intervals by
the application).
> Being
> > concerned about resource cleanup, I naturally (
 )
thought about their
> > disposal. However, static classes do not allow
finalizers to be defined.
> >
> > In one sense that makes sense - the reason for
having a static class that
> > manages these resources is that they are likely to
be required repeatedly
> > right up until the app shuts down. However I am a
bit nervous - this
> means
> > I'm entirely dependant on Windows to release all
these resources when the
> > process ends, and although Windows should do that,
to be honest I'm not
> > 100% sure I trust it to do so, I'd feel more
comfortable if my managed
> > code contained something to explicitly ensure
these resources are
> > released. (I trust .NET rather more than I trust
the Windows OS  )
> >
> > Any thoughts? Am I being too cautious when I
should just trust Windows to
> > handle resource release, or is there some other
way to deal with this
> > situation?
> >
> > ===================================
> > This list is hosted by DevelopMentor(R)  http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> http://discuss.develop.com

> >
>
>
> --
> It is the mark of an educated mind to be able to
entertain a thought
> without accepting it.
>
> ===================================
> This list is hosted by DevelopMentor(R)  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

>

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com


[1-10] [11-15]

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