Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

WeakReference/GC/multithread question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Thibaud Bouquely - 27 Dec 2004 10:51 GMT
Hello !

I just read source code witch use WeakReference object to implements some
Cache system.

I can read the classic code like this

// point A
if (myWeakReference.IsAlive)
// point B
 ((MyClass)myWeakReference.Target).DoSomething();
// point C

I am trouble with theses code Lines :
 I don't know if it is the main thread to do sometime GC.Collect() or
another thread ?
 If it is another Thread, I think we need a critical section between "Point
A" and "Point C"

moreover, It is possible we use this technic with Compact Framework
application. >> do you need to be careful about something else ?

Thanks for your help !
Jon Skeet [C# MVP] - 27 Dec 2004 14:39 GMT
> I just read source code witch use WeakReference object to implements some
> Cache system.
[quoted text clipped - 10 lines]
>   I don't know if it is the main thread to do sometime GC.Collect() or
> another thread ?

More likely is that another thread will cause a garbage collection
implicitly. I'm not entirely sure what your question is here, but any
thread can trigger a garbage collection.

>   If it is another Thread, I think we need a critical section between "Point
> A" and "Point C"
>
> moreover, It is possible we use this technic with Compact Framework
> application. >> do you need to be careful about something else ?

I suggest just using WeakReference.Target and testing the result of
nullity after retrieving it. That way you've got the reference before
you do the test, so if it's alive, you don't need to worry about it
then being GC'd.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Bruno Jouhier [MVP] - 27 Dec 2004 18:20 GMT
Yes, there is a potential race condition here.

You can easily get around it with:

   MyClass myObj = (MyClass)myWeakReference.Target;
   if (myObj != null)
       myObj.DoSomething();

Bruno

> Hello !
>
[quoted text clipped - 20 lines]
>
> Thanks for your help !
Thibaud Bouquely - 29 Dec 2004 18:06 GMT
Thanks for yours responses

We could say "MyWeakReference.IsAlive" is not very useful, no ?

the only "good" way to know is a target is alive is to use your technic ...

>> with Reflector, In WeakReference.IsAlive, we can read this
...
return (GCHandle.InternalGet(num1) != null);

and in WeakReference.Get_Target we can read too :
object obj1 = GCHandle.InternalGet(num1);
return obj1;

>> it is another fact to confirm your solution ...

> Yes, there is a potential race condition here.
>
[quoted text clipped - 30 lines]
> >
> > Thanks for your help !
Bruno Jouhier [MVP] - 29 Dec 2004 18:53 GMT
> Thanks for yours responses
>
> We could say "MyWeakReference.IsAlive" is not very useful, no ?

Agreed! I have used weak references a number of times, and I never used this
call.

Bruno.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.