![]() |
![]() |
|
|||||||
| Register | Forum Rules | Getting Started! - Guide | Blog | Videos | Gallery | Members List | Social Groups | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
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/ |
|
|
|
#2 |
|
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 |
|
|
|
#3 |
|
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 |
|
|
|
#4 |
|
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 |
|
|
|
#5 |
|
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 |
|
|
|
#6 |
|
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 |
|
|
|
#7 |
|
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 |
|
|
|
#8 |
|
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 news p.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/ |
|
|
|
#9 |
|
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 news p.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/ |
|
|
|
#10 |
|
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 |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
< Home - Windows Help - MS Office Help - Hardware Support >
| New To Site? | Need Help? |