erstellt am 17.05.2009
There are two ways in C# for safely freeing resources, these are try-finally and the using-statement. Both call the Dispose Method introduced by the IDisposable Interface, ensuring that there is no resource leakage in case of an exception. In Managed C++ there exists no using statement. Fortunately, Managed C++ is rich enough to extend itself by using templates. In this article I will explain something about resource management and how to introduce the using statement into C++/CLI.
The Common Language Runtime (CLR) is the fundamental part of the .NET Framework and executes the Intermediate Language (IL) code to which all .NET languages are compiled to. Being the base for these languages, it determines also the borders for concepts and features a language is able to provide. Thereby the CLR provides only common functionalities and does not provide resource management out of the box. This would result in a further constraint of all other languages built upon it.
Fortunately, the .NET Framework has introduced a pattern for resource management. This pattern provides the IDisposable interface. It defines that types which encapsulate resources should provide a Dispose method to free resources if they are no longer needed.
Unlike C++, C# is a language which does not have the concept of destructors. Therefore it uses try-finally and the using statement to free resources in a reliable and safe manner. This means the resources will be freed despite of a thrown exception.

The using statement creates an object within an own scope. If the execution gets out of scope, the object’s Dispose-Method will be invoked and this results in freeing the resources.
Microsoft extended Managed C++ with the try-finally statement, but there exists no using statement. Now, you probably ask yourself, why do we need try-finally when there are destructors?
That is because Managed C++ is built upon the CLR and the CLR does not provide the concept of destructors, therefore something magically happens. The language pretends the use of destructors by using the same syntax. When the code gets compiled to IL the compiler emits automatically the necessary IL code to implement the Dispose method which serves as a destructor.
As you can see in the picture the object implements the IDisposable::Dispose method. This is the reason why it is not legal in C++/CLI to explicitly implement the Dispose method.

At next I will show you a snippet which introduces the using statement in C++/CLI. The following template does only one thing; it calls the destructor of some object when the template itself gets destructed.

Before I will explain how it works I will give an example of how to use it.

What happens here and how does it work?
If you execute the example code an object of the template will be created on the stack. This means, when the execution gets out of the scope the template object’s destructor gets called. Because of the handle to the DisposableObject created on the heap, the template object is able to call its Dispose-Method in order to free resources. Because the template object is created on the stack its destructor will be also called in case of an exception. The brackets around the statement are used to define a scope in C++.
If you compare the two statements between Managed C++ and C# you will recognize the similarity. You can download the code snippet here for further explorations.
 |
3 Kommentare |
|
|
|