Timer disposed before getting to my destructor?

  • Thread starter Thread starter Jim H
  • Start date Start date
J

Jim H

I have a class object that creates and uses a System.Threading.Timer. In
the destructor for my class I cancel the timer using timer.Change(infinite,
infinite) then I call timer.Dispose() to clean it up. I get an exception
about unable to access a disposed object when I call timer.Change. This is
only happening on my notebook and not my desktop and only when running in
the debugger.

Machines are both WX Pro SP2. VS.NET 2003, .NET 1.1 SP1

Thanks,
jim
 
Hi Jim

Don't access other objects in a finalizer (destructor). When your object's finalizer is run, the Timer object may have already been finalized. I would recommend your object
implement the IDisposable interdace, and call timer.Dispose in your object's Dispose.

-Chris
--------------------
From: "Jim H" <[email protected]>
Subject: Timer disposed before getting to my destructor?
Date: Mon, 8 Nov 2004 13:20:16 -0500
Lines: 13
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <#Ac7Z#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: crlspr-24.233.164.226.myacc.net 24.233.164.226
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:285267
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I have a class object that creates and uses a System.Threading.Timer. In
the destructor for my class I cancel the timer using timer.Change(infinite,
infinite) then I call timer.Dispose() to clean it up. I get an exception
about unable to access a disposed object when I call timer.Change. This is
only happening on my notebook and not my desktop and only when running in
the debugger.

Machines are both WX Pro SP2. VS.NET 2003, .NET 1.1 SP1

Thanks,
jim


--

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
One question, what's a destructor supposed to be used for if I shouldn't
access member objects? I thought the purpose of a destructor was to do
cleanup for the current object.

jim


"Chris Lyon [MSFT]" said:
Hi Jim

Don't access other objects in a finalizer (destructor). When your
object's finalizer is run, the Timer object may have already been
finalized. I would recommend your object
implement the IDisposable interdace, and call timer.Dispose in your
object's Dispose.

-Chris
--------------------
From: "Jim H" <[email protected]>
Subject: Timer disposed before getting to my destructor?
Date: Mon, 8 Nov 2004 13:20:16 -0500
Lines: 13
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <#Ac7Z#[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: crlspr-24.233.164.226.myacc.net 24.233.164.226
Path:
cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.csharp:285267
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

I have a class object that creates and uses a System.Threading.Timer. In
the destructor for my class I cancel the timer using
timer.Change(infinite,
infinite) then I call timer.Dispose() to clean it up. I get an exception
about unable to access a disposed object when I call timer.Change. This
is
only happening on my notebook and not my desktop and only when running in
the debugger.

Machines are both WX Pro SP2. VS.NET 2003, .NET 1.1 SP1

Thanks,
jim


--

This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included script samples are subject to the terms specified
at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
Jim H said:
One question, what's a destructor supposed to be used for if I
shouldn't access member objects? I thought the purpose of a
destructor was to do cleanup for the current object.

Only cleanup which the contained objects can't do for themselves.
Usually, you don't need a destructor unless you *directly* contain
unmanaged resources. The destructors for streams etc can take care of
things themselves - but implement IDisposable whether you contain the
unmanaged resources directly or indirectly.
 
Thanks

jim

Jon Skeet said:
Only cleanup which the contained objects can't do for themselves.
Usually, you don't need a destructor unless you *directly* contain
unmanaged resources. The destructors for streams etc can take care of
things themselves - but implement IDisposable whether you contain the
unmanaged resources directly or indirectly.
 
Back
Top