calling the base destructor.

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

I create 2 ManagedC++ class inherithing from each other.
I can't see how do I call super destructor or ensure it's called :-/
template <class T>
public ref class CArray : System::IDisposable
{
protected:
~CArray()
{
if( ptr ) // I'm not sure of what IDisposable will do...
free( ptr );
ptr = nullptr;
}
private:
int length;
T* ptr;
};
template <class T>
public ref class CVector : CArray<T>
{
protected:
~CVector()
{
::~CArray();
}
};
 
You don't. C++ is not Pascal. The base destructor is called
automatically, always in the opposite order of the construction. In
other words, C++/CLI destructors work like C++ destructors from this aspect.

You don't have to (= shouldn't) inherit from IDisposable. It's
automatically done when a ref class has a destructor.

Sample code:

using namespace System;

ref class B
{
public:
~B() { Console::WriteLine("~B"); }
};

ref class D : public B
{
public:
~D() { Console::WriteLine("~D"); }
};

int main(array<System::String ^> ^args)
{
{ D d; }
Console::ReadKey(true);
return 0;
}

This outputs
~D
~B

as it would in native C++.

Note that if you allocate the class with gcnew, you must delete it, just
like in native C++:

D* d = gcnew D;
[...]
delete d;

You may want to use a finalizer in order to ensure that the native
resources are destroyed even if the caller forgets about delete. Also
note that in other languages programmers call the Dispose() method
instead of the delete operator, such as d.Dispose() in C#.

Tom
 
Note that if you allocate the class with gcnew, you must delete it, just
like in native C++:

D* d = gcnew D;
[...]
delete d;
ghh... what's the point?
I mean I write
D^ d = gcnew D();

(and not D* = gcnew D(), beside I didn't know you could create D* with
gcnew, I have to test)
and I expect it to be collected, no?
all this work for naught otherwise!
 
D* d = gcnew D;
[...]
delete d;
tested.
apparently it's illegal to write
D* d with managed type.
simple as that!

however, although it's legal to write
D^ d;
delete d;
but I would find it quite.... awful? bad design? badly choosed naming?
to HAVE TO delete MANAGED reference.
however I understand it might be as usefull (or equivalent to?) Dispose()?

And also.. yeah that's right!
Even though I removed inherit from IDisposable I could call Dispose on my
object!! nice ^_^,
I could even used it as an instance of IDisposable^!
 
Lloyd said:
D* d = gcnew D;
[...]
delete d;
tested.
apparently it's illegal to write
D* d with managed type.
simple as that!

however, although it's legal to write
D^ d;
delete d;
but I would find it quite.... awful? bad design? badly choosed naming?
to HAVE TO delete MANAGED reference.
however I understand it might be as usefull (or equivalent to?)
Dispose()?

You must call delete IF (and only if) you require deterministic destruction
of your object (e.g. if you're holding native resources). If your object
doesn't have any deterministic destruction requirement, you can just abandon
it and it will be collected.
And also.. yeah that's right!
Even though I removed inherit from IDisposable I could call Dispose
on my object!! nice ^_^,
I could even used it as an instance of IDisposable^!

Yep. That's by design.

-cd
 
Back
Top