C++/CLI: Instantiating Managed Objects
Dispose and Finalize with C++/CLI, C# and VB

C++/CLI and IDisposable

With my previous post about Instantiating Managed Objects with C++/CLI you could read about using ref types two different ways: with local variables and with a handle.

Now I'm adding implementing the IDisposable interface with the Test class:

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

    void Foo()
    {
        Console::WriteLine("Inner.Foo");
    }
};

With C++/CLI the IDisposable interface is implemented with the destructor syntax.

Now it would be good to do a try/finally to dispose the object. With C++/CLI the object is disposed using delete.
Test^ t = gcnew Test();
try
{
    t->Foo();
}
finally
{
    delete t;
}

With local variables the same can be done with fewer lines of code. This is shorter than the C# using statement!

Test t;
t.Foo();

The generated code is nearly the same. Nearly as in the first version the IL code uses the IL .try/finally statements, whereas the second version uses IL .try/fault and disposes the object in the fault and the default case.

Christian

Comments

Feed You can follow this conversation by subscribing to the comment feed for this post.

Christian Nagel's OneNotes

AUTHOR: Christian Nagel's OneNotes
IP: 000.000.000.000
DATE: 01/15/2005 10:39:00 AM

Miguel

Thanks a lot for this explanation (the clearest I've found so far). I still have a question though: If both lines come practically to the same, how do you decide if you should instantiate the object using gcnew or not ? Thanks and regards, Miguel.

Miguel

Just to clarify, I meant, how do you decide whether you should do: Test^ t = gcnew Test(); t-Foo(); OR Test t; t.Foo(); Because the difference seems so small between the two. Thanks and regards, Miguel.

Christian Nagel

Miguel, the first version Test^ t = gcnew Test(); t-Foo(); should be used if you need t for a longer time, e.g. return it from the method where this construct is used. If the instance of t is needed only within a code block, you should go for Test t; t.Foo(); As soon as the scope of t ends, Dispose is invoked. Christian

Miguel

Thanks a lot Christian for your very clear answer ! BR, Miguel.

coach outlet online

I agree you the up.As it give me the same some feeling!

uggs outlet

uggs outlet Clearly lines promotion regularly starts small in which on the way to allow them up so that it will magazine advertising campaigns Cheap GHD , form-Published vehicles uggs outlet , ugg boots on sale cheap That"s just can charge ads as hbills.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)