473,394 Members | 1,724 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,394 software developers and data experts.

Can a virtual method be called in a C# destructor

I'm reading the book "Programming .NET Components" by Juval Lowy, a renowned
..NET expert as recognized by MSFT. In the book he provides a base class
example that implements a pattern for safely cleaning up your objects
(handling "IDisposable", etc.). His class calls a (do-nothing) virtual
"Cleanup()" function from its destructor however (we're supposed to override
it) and I'd like to confirm that this is in fact safe. Presumably it is but
it's not in C++ for good reason (since objects are destroyed from the
bottom-up). This would make sense in any language but I've read elsewhere
that virtual methods aren't illegal in C# constructors (though their safety
is another matter). Can anyone comment on this situation in particular. Is
it safe to call a virtual "Cleanup()" function from a base class destructor.
Thanks.
Aug 3 '07 #1
2 1392
Hi Robert,

In C# I they are called Finalizers, not Destructors.

That is safe because the garbage collector takes two passes to collect
an object with a finalizer. The first pass calls the finalizer, and
the second pass collects the object.

More on finalizers:
http://dn.codegear.com/article/29365#CSharpFinalizers

-James

Aug 3 '07 #2
In C# I they are called Finalizers, not Destructors.
>
That is safe because the garbage collector takes two passes to collect
an object with a finalizer. The first pass calls the finalizer, and
the second pass collects the object.

More on finalizers:
http://dn.codegear.com/article/29365#CSharpFinalizers
Thanks for the info. After further reading I discovered that a C# destructor
is simply a glorified (exception-handling) wrapper around "Finalize()". I'll
take a detailed look at the link you provided however. It looks very
informative. Thanks again.
Aug 3 '07 #3

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

Similar topics

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"...
7
by: Calvin Lai | last post by:
Hi all, I have a simple question. If I have a ClassA as base class, and ClassB derive from it. There is a virtual function foo() in ClassA, and in Class B, I defined a function called foo() as...
39
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface,...
23
by: ctick | last post by:
A reason for declaring a "virtual destructor" for a Base class is to make sure the destructor of Derived class will be invoked when a pointer of Base type is used to delete an object of Derived. ...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
26
by: pmizzi | last post by:
When i compile my program with the -ansi -Wall -pedantic flags, i get this warning: `class vechile' has virtual functions but non-virtual destructor, and the same with my sub-classes. But when i...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
8
by: Shraddha | last post by:
What is the use of "PURE vitual distructors"? And why we can not have vitual constructors?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.