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

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance




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 ReferenceCountObject 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

.



Relevant Pages

  • Re: events and object lifetime
    ... onto the objects with a direct reference and had a method to set the ... Whether you use Dispose() or some other method name, ... hanging around until your application terminates and a "true" memory ... garbage collector and concentrate on programming your application. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Events unsubscribing and resource leaks?
    ... If you call an items dispose method, ... Setting a reference variable to null has no effect on ... If in your case you always know when your subscribing ... I then unsubscribe those instances that are subscribed to that, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C++/CLI is the way to go
    ... But the point is the smart pointer either doesn't need to know if it allocates the reference counter value externally, or it knows that the object is a reference counted one if you inherited from a IReferenceCounted interface or use attributes to add such code. ... But as I wrote in my other post, if you replace * with ^ and delete with Dispose it should also compile with managed ones. ... But I suppose the CLR teams would argue that there would be too much going on under the hood and it wouldn't be clear to the programmer. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Memory Leak Experts!!!!
    ... pushed as a reference across the wire to some calling program on the ... doesn't influence the behavior of the collector. ... > you also implement the Finalize method which simply calles the Dispose ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Best Practices Questions - Sending objects to/from a class modules/functions, etc?
    ... >> class module? ... Passing a Reference Type ... > Passing a Reference Type by Val passes a reference to the object, ... I can dispose of the returned object when I'm ...
    (microsoft.public.dotnet.general)