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 / November 2006

Tip: Looking for answers? Try searching our database.

Garbage collection and async operations

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 30 Nov 2006 18:49 GMT
Suppose I have the following code:

 ' Get a new instance of some class
 Dim myWorker As New MyWorker

 ' Start a long-running operation on a background thread.
 dim deleg as new SomeMethodDeleg(AddressOf myWorker.SomeMethod)
 deleg.BeginInvoke(...)

 ' Abandon the object instance
 myWorker = Nothing

At this point I would expect my MyWorker object to be eligible for garbage
collection.  But it's still busy running code on a background thread.  What
happens when the GC runs?
Jon Shemitz - 30 Nov 2006 20:04 GMT
>   ' Get a new instance of some class
>   Dim myWorker As New MyWorker
[quoted text clipped - 9 lines]
> collection.  But it's still busy running code on a background thread.  What
> happens when the GC runs?

Your expectation is wrong. The ThreadPool thread has a reference to
your delegate (it has to, to Invoke it) and so the delegate will
survive until you call EndInvoke.

Signature

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.

Bob Altman - 30 Nov 2006 20:48 GMT
I'm not sure I understand the ramifications of your answer.  Let me change
the question slightly and see what shakes out.  Suppose I have a class that
provides its own BeginXxx/EndXxx implementation:

Class Test
 Public Sub DoWork()
   ' <Perform some long-running operation>
 End Sub

 Private _deleg As New TestDelegate(AddressOf DoWork)

 Public Function BeginDoWork( _
 ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
   Return _deleg.BeginInvoke(callback, state)
 End Function

 Public Sub EndDoWork(asyncResult As IAsyncResult)
   _deleg.EndInvoke(asyncResult)
 End Sub
End Class

Now, my main program does the following:

 Dim test As New Test
 test.BeginDoWork(Nothing, Nothing)
 test = Nothing

Does something keep the (now abandoned) Test instance alive forever (since
I'm never calling the delegate's EndInvoke)?
Jon Shemitz - 30 Nov 2006 21:46 GMT
> Does something keep the (now abandoned) Test instance alive forever (since
> I'm never calling the delegate's EndInvoke)?

Actually, I was in a bit of a rush to get out the door to an
appointment, and I misspoke, slightly.

The ThreadPool thread that's executing your delegate obviously has to
have a reference to the delegate that it's executing, so clearly the
delegate can't be collected until it returns and the ThreadPool thread
is returned to the pool. (Presumably waiting threads have their
delegate-to-execute field cleared, precisely so that the delegates can
be collected.)

You should always call EndInvoke when you execute a delegate
asynchronously. The documentation is very firm about that, even if
it's not clear precisely why. Fwiw, I believe William Sullivan is
wrong about not calling EndInvoke causing a memory leak: the thread is
presumably returned to the pool as soon as the delegate returns, and
if you simply ignore the IAsyncResult that BeginInvoke returns, it
will ultimately be finalized and collected.

Take away messages:

1) Abandoning your MyWorker delegate will not cause premature
collection.

2) Not calling EndInvoke will not force the delegate to be immortal.

3) You should always call EndInvoke when you BeginInvoke a delegate.
If you don't want to bother, use ThreadPool.QueueUserWorkItem instead
of asynch execution.

Signature

.NET 2.0 for Delphi Programmers
www.midnightbeach.com/.net
What you need to know.


Rate this thread:







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.