Re: events and object lifetime
- From: "Bruce Wood" <brucewood@xxxxxxxxxx>
- Date: 15 Feb 2006 15:24:21 -0800
Nick Hounsome wrote:
The only problem with using IDisposable in this case is that it
violates a design rule for .NET, which is that it's never _necessary_
to call Dispose() in order to free an object.
I only say IDisposable because it has extra language support.
You could use any method.
.... and you would be faced with the same problem.
You are not violating a design rule about resources anymore than if you held
onto the objects with a direct reference and had a method to set the
reference to null.
Not true except for static references. If you held a direct reference
to the objects, then they would be collected when that reference were
to go out of scope. Static references, of course, never go out of
scope.
The point is still that you would have make a conscious effort to _do_
something in order that the object(s) be collected. The design ideal in
..NET is that simply having things go out of scope causes them to be
collected. Whether you use Dispose() or some other method name, you're
still left in the position of having to take some explicit action to
free the objects for collection which, if you fail to take it, leaves
them allocated for the lifetime of the running application, which is
probably not what you intended.
No - true memory leaks are impossible in .NET - the objects are accessible
through the ItemDeleted event and hence are not leaks.
Sorry. I fail to see the difference between an object that is left
hanging around until your application terminates and a "true" memory
leak. If I have a 24 x 7 application, and it continues to eat more and
more memory until it finally exhausts my system's VM and crashes, and
someone were to cheerfully point out, "But it's not a _true_ memory
leak!" I would thump them. :) Stuff that (inexplicably) doesn't get
garbage collected is a memory leak, regardless of the mechanism at work
and the level of abstraction.
What I mean by "level of abstraction" is that at a very low level of
abstraction you are correct: the objects are still accessible using
some little-known features of .NET. However, from the level of
abstraction of my program, the objects have gone out of scope but will
never be GC'd. From that point of view, it's a leak.
Weak references are a time/space trade off, no more and no less.
Not true. Weak references allow you to leave object disposal to the
garbage collector and concentrate on programming your application.
Using IDisposable, or some other disposal mechanism, requires that you
do proper memory housekeeping in order to avoid leaks (or, if you
prefer, "stuff not being garbage collected," which I maintain is
essentially the same thing). Weak references allow you to think about
static event subscribers in the same way you think about other objects
in your program: when they go out of scope, they "go away" (sooner or
later) and the memory they occupy is freed for reuse. Requiring
explicit disposal requires that you think about static event
subscribers differently: "This subscribes to a static event so I have
to explicitly dispose of it or it won't be GC'd."
Weak reference delegates allow you to decide whether subscribing to a
certain event "counts" for purposes of garbage collection. My Weak
Reference event handler wrappers allow you to express something that
native C# doesn't allow you to express (but I wish that it did): "I
want to subscribe to this event, but this subscription isn't so
important that it should keep me alive." You make your wishes known in
that regard at the moment of event subscription, and then never worry
about it again.
If you go the Dispose() route, then you have to make your wishes known
at the moment the subscribing object is no longer needed, which may not
be all that easy to determine. It leads you into strategies like
reference counting, which leads back to... C++ and manual memory
management, which flies in the face of C#'s set-it-and-forget-it,
garbage-collected mindset.
That, to me, far outweighs the time / space aspects.
.
- Follow-Ups:
- Re: events and object lifetime
- From: Nick Hounsome
- Re: events and object lifetime
- References:
- events and object lifetime
- From: anonymous.user0@xxxxxxxxx
- Re: events and object lifetime
- From: Nick Hounsome
- Re: events and object lifetime
- From: Bruce Wood
- Re: events and object lifetime
- From: Nick Hounsome
- events and object lifetime
- Prev by Date: Re: Deciphering data
- Next by Date: Re: Deciphering data
- Previous by thread: Re: events and object lifetime
- Next by thread: Re: events and object lifetime
- Index(es):
Relevant Pages
|
Loading