Feng26

I have a C# program instanciating a managed C++ object. When I am done with it, I want to call the C++ class' destructor. I have the destructor defind like the following:

...

public:

~MyMCClass()

{

...

}

...

Now my question is, how do I invode this destructor from my C# code I tried both:

...

MyCObj.~MyMCClass();

...

and

...

MyCObj.delete();

..

But none of those compiles.

How do I do this

Thanks

Lifeng



Re: Visual C++ General From C#, how to call a C++ object's destructor?

Jonathan Caves - MSFT

C# doesn't have destructors in the C++ sense - so you need to call the Dispose() method:

Code Snippet
MyCObj.Dispose();

Under the covers the C++ compiler wires implements the "Dispose Pattern" so this should work correctly.






Re: Visual C++ General From C#, how to call a C++ object's destructor?

Feng26

Thanks! I will try that. But I don't have a Despose() method in my C++ class. Do I need to create one, or somehow the destructor will get called when I call Despose()





Re: Visual C++ General From C#, how to call a C++ object's destructor?

Feng26

I just got a compile error where I call Despose() saying that the method is not defined.





Re: Visual C++ General From C#, how to call a C++ object's destructor?

Jonathan Caves - MSFT

The C++ compiler will take your destructor (and the finalizer if you have one) and wire it up with an implementation of IDisposable.Dispose to correctly create the .NET "Dispose Pattern". So while you can't see the Dispose method in your code it is there. This article might help explain what is going on.




Re: Visual C++ General From C#, how to call a C++ object's destructor?

Jonathan Caves - MSFT

It is "Dispose" not "Despose".






Re: Visual C++ General From C#, how to call a C++ object's destructor?

Feng26

Oops! Sorry. I will try again..





Re: Visual C++ General From C#, how to call a C++ object's destructor?

Feng26

I corrected the typo but somehow I still get the same complie error. By the way, I am actually calling C++ not from C#, but COBOL.Net (Fujitsu). But that shouldn't matter, right I am also calling other C++ functions of the same C++ object and from the same COBOL.Net code and they run OK.

Thanks

Feng





Re: Visual C++ General From C#, how to call a C++ object's destructor?

Jonathan Caves - MSFT

It should not matter: to the COBOL compiler it should look like the C++ class has a Dispose method. What variant of the C++ language are you using Managed C++ (lot's of __gc etc.) or C++/CLI (ref class etc.)






Re: Visual C++ General From C#, how to call a C++ object's destructor?

Feng26

I think I am using Managed C++. I know I use __gc defining my class. BTY, I just looked into the C++ assembly using ILDASM and I see, under that class, I have .ctor, {.dtor}, Finalize and all my other functions, but not Dispose.

Am I missing something

Feng





Re: Visual C++ General From C#, how to call a C++ object's destructor?

Jonathan Caves - MSFT

Unfortunately the "Dispose Pattern" feature I mentioned above is only supported by C++/CLI. For Managed C++ I would just call the Finalize method:

Code Snippet
MyCObj.Finalize();






Re: Visual C++ General From C#, how to call a C++ object's destructor?

Feng26

I think that's fine. As long as I can do my clean-up thing in the Finalize function as I normally do in the destructor. So I guess in my C++ class instead of doing:

~MyCClass()

{

deleat oOtherObjs;

}

I should do:

!MyCClass()

{

deleat oOtherObjs;

}

Am I right

Thank you so much!

Feng





Re: Visual C++ General From C#, how to call a C++ object's destructor?

Jonathan Caves - MSFT

No: in Managed C++ (as opposed to C++/CLI) the destructor (~MyClass) is the Finalize method. The compiler generates a separate {dtor} method that is called if you do delete pMyClass. All the {dtor} method does is call GC:Tongue TieduppressFinalize and then calls the destructor, which is now called Finalize.






Re: Visual C++ General From C#, how to call a C++ object's destructor?

Feng26

Hi Jonathan,

I tried calling Finalize but getting "Error 193 Method Finalize cannot be referred to from this program or class due to its accessibility." So looks like I can't call either Dispose nor Finalize in managed C++. What should a client program do to do the clean-up then

Thanks

Feng





Re: Visual C++ General From C#, how to call a C++ object's destructor?

Jonathan Caves - MSFT

It will be cleaned - eventually - that is what the garbage collector is for. What is the access of the destructor In Managed C++ it needs to be 'public public' in order to be accessible from another .NET language.