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

Tip: Looking for answers? Try searching our database.

Find out what called Finalize?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike Eaton - 06 Sep 2006 18:49 GMT
Hi everyone,

I have a long-running application (sits in the system tray waiting for
something to do and interacts with the user via form and some hardware)
containing an object (a reference to the aforementioned hardware and its
associated functionality) that occasionally will become 'nothing' i.e. when I
attempt to access it after a period of time, I get a null reference exception.

I couldn't find a path in my code that releases the object unintentionally,
so I was wondering if there was a way to tell if the garbage collector
released it or what.  This object implements IDisposable because it contains
some unmanaged resources- it wraps a device driver- but I haven't had any
luck in finding out what chunk of code called Dispose.

Any suggestions that would help me troubleshoot this problem?  I'd really
appreciate it.
Mattias Sjögren - 06 Sep 2006 20:19 GMT
Mike,

>This object implements IDisposable because it contains
>some unmanaged resources- it wraps a device driver- but I haven't had any
>luck in finding out what chunk of code called Dispose.

Can't you simply put a breakpoint in Dispose?

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Mike Eaton - 06 Sep 2006 22:00 GMT
Actually, it's not something that I've been able to reproduce while
debugging, as it is difficult to simulate the operating environment.  When
the application is under heavy use by our people it occurs more frequently.  

I can set up a log to write data to a file in the dispose method, but I'm
not sure what the most useful data to record would be.  Also, are there
situations in which the dispose method might not get called?  It's a fairly
new area for me, as I've pretty much always worked with managed code and
haven't had to worry about Dispose.

Mike

> Mike,
>
[quoted text clipped - 5 lines]
>
> Mattias
David Levine - 08 Sep 2006 13:19 GMT
You could add some diagnostic test code to capture the creator's callsite
(use the StackWalk to find it), and then write that out if the object gets
finalized or disposed. Or you could save the entire stack trace when the
object is created.

If you implement both Dispose and a finalizer you will get notified when it
either is explicitly destroyed (Dispose) or if it is no longer reachable
(the creator abandoned it) when the Finalizer is called.

So long as the object is reachable the GC will not collect it; it wont just
go away on its own.

> Actually, it's not something that I've been able to reproduce while
> debugging, as it is difficult to simulate the operating environment.  When
[quoted text clipped - 20 lines]
>>
>> Mattias

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.