Posts

Showing posts from August, 2010

C++/CLI: Destructors VS Finalizers

Spanish version / Versión en Español Even though in C# destructor and finalizer are the same thing because C# is garbage collected, in C++/CLI things are quite different. This is because C++/CLI has both deterministic (destructors) and non deterministic destruction (finalizers). Destructors are deterministically invoked when an object created on the managed stack goes out of scope; on the other hand, finalizers are called by .NET's Garbage Collector when its heuristic says so. That is why it's called non-deterministic destruction. La sintaxis: //This generates C4251 public ref class A(){ //Fields public: A(); ~A(); //Destructor !A(); //Finalizer void f(); } The destructor is automatically called when an instance of A created on the managed stack (without using gcnew/new/New) goes out of scope, and also when the Dispose method is called on an instance created on the managed heap (gcnew/New). This is because the C++/CLI compiler maps the destr

C++/CLI: Destructores VS Finalizers

Versión en inglés / English version Aunque en C# estas dos palabras representan lo mismo, dado que C# es un lenguaje con recolección de basura, en C++/CLI la cosa es muy diferente. Esto se debe a que dicho lenguaje tiene tanto destrucción determinística (destructores) como no determinística (finalizers). Los destructores son invocados determinísticamente al irse de scope el objeto; por otro lado, los finalizers son invocados por el recolector de basura de .NET cuando su heurística lo indique. Es por ello que dicha destrucción se denomina "no deterministica." La sintaxis: //Esto genera C4251 public ref class A(){ //Atributos public: A(); ~A(); //Destructor !A(); //Finalizer void f(); } El destructor es invocado cuando una instancia de A creada en el managed stack (o sea, sin usar gcnew/New/new) se va de scope, y cuando se invoca Dispose sobre una instancia creada en managed heap. Esto último es porque el compilador de C++/CLI mapea el dest

C++/CLI: Convert a String to BSTR or some other nasty stuff

Spanish version / Versión en Español This is very usual when replacing a COM interface with a C++/CLI wrapper (Guess who's doing that?). That usually leads to the need to convert a .NET string (System::String^) to a BSTR, or a std::string. Luckily, .NET provides some handy conversion functions for this. They save us the hassle of creating buffers, releasing them and whatnot. Among them are the marshal_as functions and the marshal_context class. Here's the lowdown for when to use which: Overview of Marshalling in C++

C++/CLI: Transformar un String a BSTR u otras cochinadas

Versión en inglés / English version Esto es muy común cuando uno quiere reemplazar una interfaz COM por una C++/CLI. Esto típicamente requiere convertir System::String^ a BSTR, std::string y otras yerbas. Por suerte, .NET nos provee una solución que nos evita andar copiando cosas a buffers y viendo cuándo las liberamos: El método marshal_as y la clase marshal_context. Acá está el detalle de qué usar para cada conversión: Overview of Marshalling in C++