473,811 Members | 3,055 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Should the Dispose( ) method only run when there are no other references to an object?

I understand the IDisposable interface and how the using instruction
will call the Dispose method of an object.

What if a reference to an object is passed to a 2nd thread? And the
object contains a handle to an open file. The Dispose( ) method closes
the handle.

How would the Dispose( ) method called from the end of the using block
in the 1st thread know that references to the object still exists in
other threads?

thanks,

-Steve

Jan 22 '07 #1
9 1444
Steve,

It wouldn't. But, as the programmer you would know about other
references and code accordingly for those situations. In other words,
if you allow an object to be used in other threads don't dispose it
until those threads are done with it.

Brian

Steve Richter wrote:
I understand the IDisposable interface and how the using instruction
will call the Dispose method of an object.

What if a reference to an object is passed to a 2nd thread? And the
object contains a handle to an open file. The Dispose( ) method closes
the handle.

How would the Dispose( ) method called from the end of the using block
in the 1st thread know that references to the object still exists in
other threads?

thanks,

-Steve
Jan 22 '07 #2

Brian Gideon wrote:
Steve,

It wouldn't. But, as the programmer you would know about other
references and code accordingly for those situations. In other words,
if you allow an object to be used in other threads don't dispose it
until those threads are done with it.
thanks for confirming that. I can accept that I have to program for it.
I am thinking that the absence of reference counting from .NET objects
is a failing of its design. Can I define a ReferenceCountO bject class
that is derived from Object. Then base any class of mine that I want to
maintain a reference count in from that class? How would such a class
work to update the reference count when the object is passed by
reference to a method or added to a list?

-Steve
>
Steve Richter wrote:
I understand the IDisposable interface and how the using instruction
will call the Dispose method of an object.

What if a reference to an object is passed to a 2nd thread? And the
object contains a handle to an open file. The Dispose( ) method closes
the handle.

How would the Dispose( ) method called from the end of the using block
in the 1st thread know that references to the object still exists in
other threads?

thanks,

-Steve
Jan 22 '07 #3
"Steve Richter" <St************ @gmail.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
thanks for confirming that. I can accept that I have to program for it.
I am thinking that the absence of reference counting from .NET objects
is a failing of its design.
It's not a failing, the very idea was to get rid of ref counting.
Can I define a ReferenceCountO bject class
that is derived from Object. Then base any class of mine that I want to
maintain a reference count in from that class? How would such a class
work to update the reference count when the object is passed by
reference to a method or added to a list?
This is one way you could do it. There would be plenty of other custom
methods you could use also which would all be just that, custom. Generally I
would have thought you'd have 1 thread being the owner of the object and
that thread handles to lifetime of that object. If it's passed the object to
another thread then it would know and not dispose it.

Michael
Jan 22 '07 #4


"Michael C" <no****@nospam. comwrote in message
news:eS******** ******@TK2MSFTN GP05.phx.gbl...
"Steve Richter" <St************ @gmail.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
>thanks for confirming that. I can accept that I have to program for it.
I am thinking that the absence of reference counting from .NET objects
is a failing of its design.

It's not a failing, the very idea was to get rid of ref counting.
To be fair, ref counting is semantically superior, but it's more expensive
than garbage collecting.
>
>Can I define a ReferenceCountO bject class
that is derived from Object. Then base any class of mine that I want to
maintain a reference count in from that class? How would such a class
work to update the reference count when the object is passed by
reference to a method or added to a list?
It wouldn't. Reference counting would need to be baked into the low-level
memory management of .NET so that all those reference assignments operations
correctly maintained the reference count.
David

Jan 22 '07 #5

Michael C wrote:
"Steve Richter" <St************ @gmail.comwrote in message
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
thanks for confirming that. I can accept that I have to program for it.
I am thinking that the absence of reference counting from .NET objects
is a failing of its design.

It's not a failing, the very idea was to get rid of ref counting.
that idea dates back in the way back year of 2000 when the CPU was slow
and the world thought it was at peace. Now with quad cores there are
plenty of cycles to support reference counting. Built in reference
counting makes programming easier - understanding the nuances of
finalization and disposing is not easy.

what I would like is to have an IReference interface with a method that
is called everytime a reference to an object is created. Then another
method is called when the reference goes out of scope.
>
Can I define a ReferenceCountO bject class
that is derived from Object. Then base any class of mine that I want to
maintain a reference count in from that class? How would such a class
work to update the reference count when the object is passed by
reference to a method or added to a list?

This is one way you could do it. There would be plenty of other custom
methods you could use also which would all be just that, custom. Generally I
would have thought you'd have 1 thread being the owner of the object and
that thread handles to lifetime of that object. If it's passed the object to
another thread then it would know and not dispose it.
what if the object is added to a list within the using block? Then at
the end of the block dispose( ) is called and that method has no idea
that references to the object are still alive in an array somewhere.

-Steve

Jan 22 '07 #6
Steve,

I never understand why people take program languages like C#, which does a
lot for them including finalizing, while they want in fact the features of
Intel assembler.

http://www.intel.com/design/network/...k_download.htm

Cor

"Steve Richter" <St************ @gmail.comschre ef in bericht
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
>I understand the IDisposable interface and how the using instruction
will call the Dispose method of an object.

What if a reference to an object is passed to a 2nd thread? And the
object contains a handle to an open file. The Dispose( ) method closes
the handle.

How would the Dispose( ) method called from the end of the using block
in the 1st thread know that references to the object still exists in
other threads?

thanks,

-Steve

Jan 22 '07 #7
"Steve Richter" <St************ @gmail.comwrote in message
news:11******** **************@ v45g2000cwv.goo glegroups.com.. .
that idea dates back in the way back year of 2000 when the CPU was slow
and the world thought it was at peace.
No matter how fast a processor gets an app will still run faster without ref
counting. In some cases quite significantly.
Now with quad cores there are
plenty of cycles to support reference counting.
Dual core CPUs are actually more suited to the .net model because the GC
runs in another thread.
Built in reference
counting makes programming easier - understanding the nuances of
finalization and disposing is not easy.
I don't think it's that much of an issue. When you no longer need an object
and it has a dispose method then call it. I find it *significantly* easier
than dealing with circular reference issues.
what I would like is to have an IReference interface with a method that
is called everytime a reference to an object is created. Then another
method is called when the reference goes out of scope.
AFAIK this is not possible.
what if the object is added to a list within the using block? Then at
the end of the block dispose( ) is called and that method has no idea
that references to the object are still alive in an array somewhere.
This is an issue for you to deal with. This was the same with ref counting
in a lot of cases anyway, eg an ADO recordset having a close method.

Michael
Jan 22 '07 #8
Hi Steve,

I think, you have to !design! some rule for the lifetime for all your
disposable objects; then you have to implement it by code.

One simple way to do it, is to use it only locally inside the method were
the disposable is created. This can easily be implemented by a using
statement.
A second way is, to put it in an instance field of a disposable class and
then dispose it in the dispose method of that class. This surely would only
delegate the necessaty of a lifetime rule to the referrer of that outer
class.

There are situations, when the lifetime rule has to be much more
complicated/sofisticated, but still it should be clearly designed first, and
than be implemented.

"Steve Richter" <St************ @gmail.comschri eb im Newsbeitrag
news:11******** **************@ s34g2000cwa.goo glegroups.com.. .
>I understand the IDisposable interface and how the using instruction
will call the Dispose method of an object.

What if a reference to an object is passed to a 2nd thread? And the
object contains a handle to an open file. The Dispose( ) method closes
the handle.

How would the Dispose( ) method called from the end of the using block
in the 1st thread know that references to the object still exists in
other threads?

thanks,

-Steve

Jan 22 '07 #9

Michael C wrote:
Dual core CPUs are actually more suited to the .net model because the GC
runs in another thread.
Unfortunately, the GC will suspend all application threads so that it
can perform it's work correctly. Now, depending on what version of the
CLR is installed the GC may have one thread running on each processor
or just one thread period. Regardless, the other application threads
are still suspended during the collection process. I suspect in the
future Microsoft will try to take better advantage of the multiple core
processors. Nevertheless, GC is still faster than reference counting
and there's at least a portion of it that can take advantage of
symmetric multiprocessing .

Jan 22 '07 #10

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

Similar topics

24
7706
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my Dispose-Method and call the GC nothing happend!!! my Disposemethod has never been called!! so the GC dont call my Dispose-Method although I implemented IDisposable? what am i doing wrong?
4
2038
by: xyu | last post by:
Hello, First I would like to thank anyone who helps me, much appreciated. I'm a c++ programmer just started using c#. I'm writing some big hash table so I want to actively take my object off the heap and release the memory when it's deleted from the hash table so that GC recollection does not need to run that frequently. I read up examples about Dispose method in MSDN. What I find very strange is that it's all talking about how to...
1
1735
by: Billy | last post by:
Hello... I'm trying to make a database access class for an asp.net application. When I run my application, the Garbage Collecter doesn't seems to unload the memory attributed to my SQLConnection. My db access class has a method that returns a dataset and one that executes a non-reader request. It also has a dispose method (to release the connection) that I call in the finalize method of my aspx pages(as learn in "Implementing...
11
2570
by: Leon | last post by:
Are dataset automatically stored in memory? Does the dispose() method automatically dispose of the dataset in the code below? Do I have to dispose a dataset from memory or does the dataset dispose itself when not in use? I know I can use the clear method, but that just clear the data within dataset not dispose of the dataset all together right?
6
1812
by: Teresa | last post by:
1) If I do want to keep an object alive throughout the live of an application, how can I ensure that the GC doesn't clean it up? 2a) How do I determine if an object is a managed or an unmanged resource? I understand the basic definition for managed resources are resources that the CLR manage and unmanged resources are resources that the CLR doesn't manage, however, I haven't been able to find a concrete answer as to what resources are...
1
3697
by: Brad Wood | last post by:
If I run this console app (outside the debugger), I will see both Console.WriteLine statements: class Program { class MyClass: IDisposable { public void Dispose() { Console.WriteLine( "MyClass dispose called" );
13
1677
by: Dave | last post by:
Could someone explain to me when it is appropriate to call the Dispose() method that is contained in most .Net Framework Objects. The MSDN says that Dispose: Releases all resources used by the Object and or Releases the unmanaged resources used by the Object and optionally releases
1
1919
by: kuhrty | last post by:
Hi, I am creating a multi-winform project. Everything has been working fine, except after I perform an update to the database and decide to Exit the winform after the update operation is complete. I have error handling in my code but not on the dispose method. I call the dispose method exiting the application using the code below but results in this error message "An unhandled exception of type 'System.NullReferenceException'
4
2845
by: fabrice | last post by:
Re hello, I m' sorry for my questions .. Under framework 1.1 with vb.net, i m using a StringWriter object to export in .xls file. To empty memory, I would like to use the propterty dispose on the object StringWriter. But i have received an error like this : example :
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3020
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.