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

gc classes and destructors

Hi,

Below is a smal test program which create two objects deriving from
DataTable.

When running the 3 lines of code marked as Ex1 the destructor of DataTableEx
is never called,- why?

When running the 2 lines of code marked as Ex2 the first line fail
compilation claiming there is no destructor,- why ? (the second one force by
destructor to be called)

The documentation I've read says that the compiler will "wrap" my destructor
into a finalize method that should be called my the gc.

Your help is highly appreciated,- thanks

Peter Hemmingsen

#using <mscorlib.dll>
#using <System.dll>
#using <System.Data.dll>

using namespace System::Data;

public __gc class DataTableEx : public DataTable {
public:

DataTableEx():DataTable() {
System::Console::Write ("DataTableEx created\n");
};
~DataTableEx() {
System::Console::Write ("DataTableEx destroyed\n");
};
};

void main () {
DataTable* dt=new DataTableEx();
DataTableEx* dtex=new DataTableEx();

// ex 1
dt=0;
dtex=0;
System::GC::Collect();

// ex 2
delete dt; // compiler fails !!!
delete dtex;
}
Nov 17 '05 #1
4 1206
Peter Hemmingsen wrote:
When running the 3 lines of code marked as Ex1 the destructor of
DataTableEx is never called,- why?
To make sure things are clear, this is actually the finalizer that isn't
running. Unfortunately, the current syntax for C++ complicates the syntax
for writing a destructor actually generates both a destructor and a
finalizer. The destructor function that is emitted just supresses
finalization and then calls the finalizer. All the code that you write in
the class destructor goes into the finalizer function.

All that said, I have no idea why the CLR is not running the finalizers for
these objects. Studying the IL, there's absolutely no reason as far as I can
see for the behavior. I've opened a bug with the CLR.
When running the 2 lines of code marked as Ex2 the first line fail
compilation claiming there is no destructor,- why ? (the second one force
by destructor to be called)


The first class doesn't have a destructor, so there's no way to call it. The
second class (the one defined in C++) has a destructor. If you disassemble
the code using ildasm, you will see the __dtor function. That's why the
compiler lets you call it.

As soon as I hear back from the CLR about the finalizer problem, I'll let
you know.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
Nov 17 '05 #2
Hi Brandon,

Thanks a lot for your reply. I'm looking forward to hear from you.

BTW the problem only occur for classes deriving from DataTable, if I derive
from another class (such as Drawing::Image) the destructor is correctly
called.

Peter

"Brandon Bray [MSFT]" <br******@online.microsoft.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Peter Hemmingsen wrote:
When running the 3 lines of code marked as Ex1 the destructor of
DataTableEx is never called,- why?
To make sure things are clear, this is actually the finalizer that isn't
running. Unfortunately, the current syntax for C++ complicates the syntax
for writing a destructor actually generates both a destructor and a
finalizer. The destructor function that is emitted just supresses
finalization and then calls the finalizer. All the code that you write in
the class destructor goes into the finalizer function.

All that said, I have no idea why the CLR is not running the finalizers

for these objects. Studying the IL, there's absolutely no reason as far as I can see for the behavior. I've opened a bug with the CLR.
When running the 2 lines of code marked as Ex2 the first line fail
compilation claiming there is no destructor,- why ? (the second one force by destructor to be called)
The first class doesn't have a destructor, so there's no way to call it.

The second class (the one defined in C++) has a destructor. If you disassemble
the code using ildasm, you will see the __dtor function. That's why the
compiler lets you call it.

As soon as I hear back from the CLR about the finalizer problem, I'll let
you know.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.

Nov 17 '05 #3
Peter Hemmingsen wrote:
BTW the problem only occur for classes deriving from DataTable, if I
derive from another class (such as Drawing::Image) the destructor is
correctly called.


Hi Peter,
The bug on this question is still open, but there is something to report.
The constructor for System::Data::DataTable calls SuppressFinalize. This is
why the finalizer is never called for anything that derives from DataTable.

As the bug is still open, I believe the question now is why the constructor
does that. If you must work around this problem, you can reregister the
object for finalization in the derived class's constructor.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
Nov 17 '05 #4
Hi Brandon,

Thanks for getting back. If there is a reason why it call SuppressFinalize
wouldn't it then be dangerous to reregister it?

Peter

"Brandon Bray [MSFT]" <br******@online.microsoft.com> wrote in message
news:%2******************@tk2msftngp13.phx.gbl...
Peter Hemmingsen wrote:
BTW the problem only occur for classes deriving from DataTable, if I
derive from another class (such as Drawing::Image) the destructor is
correctly called.
Hi Peter,
The bug on this question is still open, but there is something to

report. The constructor for System::Data::DataTable calls SuppressFinalize. This is why the finalizer is never called for anything that derives from DataTable.
As the bug is still open, I believe the question now is why the constructor does that. If you must work around this problem, you can reregister the
object for finalization in the derived class's constructor.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.

Nov 17 '05 #5

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

Similar topics

1
by: Mike | last post by:
I seem to be having a problem with the way destructors are called with derived classes. Below is a short example of what I'm trying to do. In the example a is the base class. It has a function...
3
by: Nuno Barros | last post by:
Cn someone tell me if when i call the destructor of a derivated class, the destructor of the base class is called implicitly? Or shall i call the destructor by myself? Thanks in advance ...
14
by: Pratts | last post by:
I am a new one who have joined u plz try to help me bcoz i could not find ny sutiable answer foer this Question Qus>>why do we need classes when structures provide similar functionality??
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
26
by: Michi Henning | last post by:
I've been having problem with destructors in the context of having ported C# code developed under .NET to Mono. What happens is that, on a dual-CPU machine, various parts of the code crash randomly...
7
by: alternativa | last post by:
Hello, I have a few questions concerning classes. 1) Why some people use default constructos, i.e constructors with no parameters? To me it doesn't make any sense, is there something I should...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
4
by: Eric | last post by:
I was wondering what people thought about the information found at: http://g.oswego.edu/dl/mood/C++AsIDL.html Specifically, I am interested in the following recommendation: ---- Since...
5
by: Renato | last post by:
I have an array of pointers to class Shape. I create 4 items and display their values. shapes Shape *shapes; shapes = new Shape (p1); shapes = new Triangle (p1); shapes = new Polygon (p2);
7
by: ademirzanetti | last post by:
Hi there !!! I would like to listen your opinions about inherit from a STL class like list. For example, do you think it is a good approach if I inherit from list to create something like...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.