473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C# and garbage collection

I have got two classes, one is Device and the other is Buffer. Every Buffer
instance has an associated Device instance, since all Buffers have to be
created with Device.CreateBuffer(). Now i want to be able to notify an event
to all Buffer instances associated with a certain Device. At the same time i
would like the Buffer instances to be garbage collected, if there is no
reference from the client code (the one that called Device.CreateBuffer)
anymore. The problem is that the Device instance still holds a reference to
the otherwise unreferenced Buffer, preventing it from being garbage
collected.

I could solve the problem with a Destroy() method on the Buffer class, that
deletes itself from the notify-list of the Device instance. But having this
explicit destroy instruction is not really nice in a managed environment.

Someone's told me about WeakReference objects, but they look more like a
workaround than a solution. Any hints?
Nov 16 '05 #1
3 1728
Kyle,

Those are really the only two options you have. You wouldn't use a
Destroy method, but rather, you would implement IDisposable, and call
Dispose on the Device class. The Dispose method would notify the Device
that created it to let go.

The other way (and personally, I think more elegant way) would be to use
WeakReferences, as they are created for a situation like this. It would
prevent the need for a Dispose method. When you store the Buffers in the
Device instance, you would store WeakReferences. You could do general
cleanup every once in a while, reducing the list of WeakReferences that you
have, but that's about the only major obstacle.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kyle Kaitan" <kkaitan [at] gmail [dot] com> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
I have got two classes, one is Device and the other is Buffer. Every Buffer
instance has an associated Device instance, since all Buffers have to be
created with Device.CreateBuffer(). Now i want to be able to notify an
event
to all Buffer instances associated with a certain Device. At the same time
i
would like the Buffer instances to be garbage collected, if there is no
reference from the client code (the one that called Device.CreateBuffer)
anymore. The problem is that the Device instance still holds a reference
to
the otherwise unreferenced Buffer, preventing it from being garbage
collected.

I could solve the problem with a Destroy() method on the Buffer class,
that
deletes itself from the notify-list of the Device instance. But having
this
explicit destroy instruction is not really nice in a managed environment.

Someone's told me about WeakReference objects, but they look more like a
workaround than a solution. Any hints?

Nov 16 '05 #2
why isn't that nice? there's nothing wrong with explicitly calling
Destroy() or even Dispose() for that matter ... there's a whole
language construct dedicated to making sure Dispose() gets called
(using).

Joel Martinez
http://www.onetug.org - Orlando .NET User Group
http://www.codecube.net - blog

Nov 16 '05 #3
I wouldn't say that there is anything wrong with using Dispose(), but I
would opt for the weak ref. If you rely on Dispose, then you are relying on
the developer who uses the code to remember to call dispose. If you use the
weak ref, it will "just work".

Personally, I'm not a real fan of dispose. The problem is consistency:
Some objects implement a dispose, and some don't. If you're paying
attention, good for you, but what about the guys who aren't? The fact that
it's only there some of the time, means that there is no habit-forming (with
Delphi, for example, if you created it, you destroyed it -- easy). Further,
if you have rev 1 of an object and it does not require a dispose, and then
in a future rev, you add one, now you potentially have a bunch of code to
track down and add disposes. When you think about it in terms of lifecycle,
the dispose pattern is really kind of ugly, and it's one of the very few
things about .Net that doesn't quite sit right with me.

<jd********@ea.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
why isn't that nice? there's nothing wrong with explicitly calling
Destroy() or even Dispose() for that matter ... there's a whole
language construct dedicated to making sure Dispose() gets called
(using).

Joel Martinez
http://www.onetug.org - Orlando .NET User Group
http://www.codecube.net - blog

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and...
6
by: Ganesh | last post by:
Is there a utility by microsoft (or anyone) to force garbage collection in a process without have access to the process code. regards Ganesh
11
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind...
34
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple,...
5
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000...
8
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by...
28
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will...
56
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application =...
350
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.