473,324 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

destructor not being called

hi,

i have a class called foo that has a method called ~foo (well it might not
be a method but I'm not worried about the actual semantics here)

from what I can tell this makes it a destructor which means it should get
called when the object is disposed of by the runtime. presumably this
includes when the appdomain exits as well as when the GC needs to run to get
more memory?

however, mine is never being invoked. FWIW this is part of a windows service

anyone know what is happening here? what is the accepted best practice for
having some code run when an object is being disposed of ?

TIA

Andy
Oct 1 '07 #1
2 1736
"Andy Fish" <aj****@blueyonder.co.ukschrieb im Newsbeitrag
news:uA*************@TK2MSFTNGP04.phx.gbl...
hi,

i have a class called foo that has a method called ~foo (well it might not
be a method but I'm not worried about the actual semantics here)

from what I can tell this makes it a destructor
From C# 2.0 on they are officially called finalizers, no destructors. That's
because their semantic is very different from destructors in C++ (and maybe
other languages).
which means it should get called when the object is disposed of by the
runtime. presumably this includes when the appdomain exits
No,
as well as when the GC needs to run to get more memory?
To be more precise, when the GC has run and detected, that the instance
isn't anymore reachable by code (outside of finallizers):
>
however, mine is never being invoked. FWIW this is part of a windows
service
Why do you think you have to implement a finalizer here? They should only be
used to cleanup work, wich isn't yet done by the .NET-Runtime, like freeing
os-handles wich your code owns. This doesn't normally occur in applidatoin
code.

Christof

Oct 1 '07 #2
On Oct 1, 10:15 am, "Andy Fish" <ajf...@blueyonder.co.ukwrote:
hi,

i have a class called foo that has a method called ~foo (well it might not
be a method but I'm not worried about the actual semantics here)

from what I can tell this makes it a destructor which means it should get
called when the object is disposed of by the runtime. presumably this
includes when the appdomain exits as well as when the GC needs to run to get
more memory?

however, mine is never being invoked. FWIW this is part of a windows service

anyone know what is happening here? what is the accepted best practice for
having some code run when an object is being disposed of ?

TIA

Andy
Hi Andy,
A common reason that the finalizer (what you're calling the
destructor) might not be called is that the Dispose( ) method has
called GC.SuppressFinalize(this); -- is that possible in your
situation?

Although it's possible that the finalizer isn't getting called for
another reason, I would check this out first.
John

Oct 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
9
by: sahukar praveen | last post by:
Hello, This is the program that I am trying. The program executes but does not give me a desired output. ********************************************** #include <iostream.h> #include...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
16
by: Timothy Madden | last post by:
Hy I have destructors that do some functional work in the program flow. The problem is destructors should only be used for clean-up, because exceptions might rise at any time, and destructors...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
4
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
14
by: gurry | last post by:
Suppose there's a class A. There's another class called B which looks like this: class B { private: A a; public : B() { a.~A() } }
5
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) {...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.