TechTalkz.com Logo Ask the Expert

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Programing Languages > C#(C Sharp)

Notices

Garbage collector question

C#(C Sharp)


Reply
 
Thread Tools Display Modes
Old 11-12-2007, 06:06 PM   #1
DoB
Guest
 
Posts: n/a
Garbage collector question

Hi,

Suppose a situation where some object goes out of scope and its reference
is not kept anywhere, but a reference to some delegate (that references
one of the object's methods) is kept as a value in some map.

Is there a possibility that the object is disposed and the delegate will
be invalid?

Yours DoB

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
  Reply With Quote
Old 11-12-2007, 06:06 PM   #2
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
Re: Garbage collector question

On Dec 11, 12:58 pm, DoB <D...@dob.com> wrote:
> Suppose a situation where some object goes out of scope and its reference
> is not kept anywhere, but a reference to some delegate (that references
> one of the object's methods) is kept as a value in some map.
>
> Is there a possibility that the object is disposed and the delegate will
> be invalid?


Well, it might be disposed - but it certainly won't be garbage
collected.

The "a reference to a delegate that references one of the object's
methods" is directly contradictory to "its reference is not kept
anywhere". The reference *is* kept in the delegate - provided it's an
instance method, of course.

Jon
  Reply With Quote
Old 12-12-2007, 04:32 PM   #3
DoB
Guest
 
Posts: n/a
Re: Garbage collector question

> The "a reference to a delegate that references one of the object's
> methods" is directly contradictory to "its reference is not kept
> anywhere". The reference *is* kept in the delegate - provided it's an
> instance method, of course.


Yes, it will be an instance method.

So...
as long as the reference to the delegate is kept, the referenced method
might be safely invoked, is that right?

DoB


  Reply With Quote
Old 12-12-2007, 04:32 PM   #4
DoB
Guest
 
Posts: n/a
Re: Garbage collector question

> The "a reference to a delegate that references one of the object's
> methods" is directly contradictory to "its reference is not kept
> anywhere". The reference *is* kept in the delegate - provided it's an
> instance method, of course.


Yes, it will be an instance method.

So...
as long as the reference to the delegate is kept, the referenced method
might be safely invoked, is that right?

DoB


  Reply With Quote
Old 12-12-2007, 04:33 PM   #5
Marc Gravell
Guest
 
Posts: n/a
Re: Garbage collector question

Define "safely" ;-p

The delegate will prevent the object being garbage collected, but if
the object itself has been deliberately closed/disposed/whatever (by
code that thinks the object is no longer needed) then the method will
probably throw an exception when invoked.

This type of delegate (to an otherwise unreferenced object) is fine by
itself, but be aware that if used inappropriately this type of hanging
reference (especially events) is a common cause of memory issues -
i.e. event subscriptions keeping thousands of objects from being
collected. For example:
http://www.codeproject.com/KB/showca...SProfiler.aspx

Marc
  Reply With Quote
Old 12-12-2007, 04:33 PM   #6
Marc Gravell
Guest
 
Posts: n/a
Re: Garbage collector question

Define "safely" ;-p

The delegate will prevent the object being garbage collected, but if
the object itself has been deliberately closed/disposed/whatever (by
code that thinks the object is no longer needed) then the method will
probably throw an exception when invoked.

This type of delegate (to an otherwise unreferenced object) is fine by
itself, but be aware that if used inappropriately this type of hanging
reference (especially events) is a common cause of memory issues -
i.e. event subscriptions keeping thousands of objects from being
collected. For example:
http://www.codeproject.com/KB/showca...SProfiler.aspx

Marc
  Reply With Quote
Old 12-12-2007, 04:33 PM   #7
DoB
Guest
 
Posts: n/a
Re: Garbage collector question

> The "a reference to a delegate that references one of the object's
> methods" is directly contradictory to "its reference is not kept
> anywhere". The reference *is* kept in the delegate - provided it's an
> instance method, of course.


Yes, it will be an instance method.

So...
as long as the reference to the delegate is kept, the referenced method
might be safely invoked, is that right?

DoB


  Reply With Quote
Old 12-12-2007, 04:34 PM   #8
Rene
Guest
 
Posts: n/a
Re: Garbage collector question

If you look at the Delegate definition, you will see that it contains a
"Target" and a "Method" properties.

public object Target { get; }
public MethodInfo Method { get; }

This "Target" property is used by the delegate to hold a reference to the
object that contains the "Method" that should be called when the delegate is
invoked.

Having said that, as long as the "Target" property is pointing to the object
(call this object XYZ), the object XYZ will never be garaged collected
because there *is* a reference to this object (XYZ) in the delegate
instance.

It may look like there is no way to get a hold of the XYZ object but you
could do so by using the "GetInvocationList()" method of the delegate.



"DoB" <DoB@dob.com> wrote in message
newsp.t258zfipmqfnmo@dwroblewski.hq.abg.com.pl.. .
> Hi,
>
> Suppose a situation where some object goes out of scope and its reference
> is not kept anywhere, but a reference to some delegate (that references
> one of the object's methods) is kept as a value in some map.
>
> Is there a possibility that the object is disposed and the delegate will
> be invalid?
>
> Yours DoB
>
> --
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



  Reply With Quote
Old 12-12-2007, 04:34 PM   #9
Rene
Guest
 
Posts: n/a
Re: Garbage collector question

If you look at the Delegate definition, you will see that it contains a
"Target" and a "Method" properties.

public object Target { get; }
public MethodInfo Method { get; }

This "Target" property is used by the delegate to hold a reference to the
object that contains the "Method" that should be called when the delegate is
invoked.

Having said that, as long as the "Target" property is pointing to the object
(call this object XYZ), the object XYZ will never be garaged collected
because there *is* a reference to this object (XYZ) in the delegate
instance.

It may look like there is no way to get a hold of the XYZ object but you
could do so by using the "GetInvocationList()" method of the delegate.



"DoB" <DoB@dob.com> wrote in message
newsp.t258zfipmqfnmo@dwroblewski.hq.abg.com.pl.. .
> Hi,
>
> Suppose a situation where some object goes out of scope and its reference
> is not kept anywhere, but a reference to some delegate (that references
> one of the object's methods) is kept as a value in some map.
>
> Is there a possibility that the object is disposed and the delegate will
> be invalid?
>
> Yours DoB
>
> --
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/



  Reply With Quote
Old 12-12-2007, 04:34 PM   #10
Marc Gravell
Guest
 
Posts: n/a
Re: Garbage collector question

Define "safely" ;-p

The delegate will prevent the object being garbage collected, but if
the object itself has been deliberately closed/disposed/whatever (by
code that thinks the object is no longer needed) then the method will
probably throw an exception when invoked.

This type of delegate (to an otherwise unreferenced object) is fine by
itself, but be aware that if used inappropriately this type of hanging
reference (especially events) is a common cause of memory issues -
i.e. event subscriptions keeping thousands of objects from being
collected. For example:
http://www.codeproject.com/KB/showca...SProfiler.aspx

Marc
  Reply With Quote
Reply

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT +5.5. The time now is 07:04 PM.


vBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2010, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional