What is special about Dispose() methods?
-
Hi, I am using Visual Studio C++/CLI. I am now playing around with the System::Drawing::Font class and I have seen Microsoft examples that use Dispose() but when I try to invoke the System::Drawing::Font::Dispose() function the compiler gives me an "error C2039: 'Dispose' : is not a member of 'System::Drawing::Font'". Does anyone know why this happens? The intellisense shows that the Dispose() method is a member of the Font class (as well as the Microsoft docs). Thanks Buck
-
Hi, I am using Visual Studio C++/CLI. I am now playing around with the System::Drawing::Font class and I have seen Microsoft examples that use Dispose() but when I try to invoke the System::Drawing::Font::Dispose() function the compiler gives me an "error C2039: 'Dispose' : is not a member of 'System::Drawing::Font'". Does anyone know why this happens? The intellisense shows that the Dispose() method is a member of the Font class (as well as the Microsoft docs). Thanks Buck
Dispose()
is a function inherited from theIDisposable
interface. Its meant to implement the "Dispose pattern" to enable some form of immediate destructors in managed code. In C# you can callDispose()
on objects that will perform some clean up functions (like closing streams and such). In C++/CLIDispose()
is mapped to the class destructor by the compiler. You can't explicitly invokeDispose()
(nor write one). Instead, if an object encapsulating a resource needs some clean up done immediately (rather than waiting for the garbage collector) call either aClose()
type function (which itself should only call the destructor I believe) or usedelete
(or declare the variable with stack semantics and have the destructor called automatically when the variable goes out of scope). -
Hi, I am using Visual Studio C++/CLI. I am now playing around with the System::Drawing::Font class and I have seen Microsoft examples that use Dispose() but when I try to invoke the System::Drawing::Font::Dispose() function the compiler gives me an "error C2039: 'Dispose' : is not a member of 'System::Drawing::Font'". Does anyone know why this happens? The intellisense shows that the Dispose() method is a member of the Font class (as well as the Microsoft docs). Thanks Buck
This bit me a bit when I recently migrated to VS 2005. Here's an article that may help (even though the article is about porting to 2005, it contains relevant info about destructors and Dispose): Changes in Destructor Semantics[^] Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Dispose()
is a function inherited from theIDisposable
interface. Its meant to implement the "Dispose pattern" to enable some form of immediate destructors in managed code. In C# you can callDispose()
on objects that will perform some clean up functions (like closing streams and such). In C++/CLIDispose()
is mapped to the class destructor by the compiler. You can't explicitly invokeDispose()
(nor write one). Instead, if an object encapsulating a resource needs some clean up done immediately (rather than waiting for the garbage collector) call either aClose()
type function (which itself should only call the destructor I believe) or usedelete
(or declare the variable with stack semantics and have the destructor called automatically when the variable goes out of scope). -
This bit me a bit when I recently migrated to VS 2005. Here's an article that may help (even though the article is about porting to 2005, it contains relevant info about destructors and Dispose): Changes in Destructor Semantics[^] Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Thank you for bringing the subject up....it reminded me I needed to finish some porting of my code from managed extensions (VS 2003). In case you haven't already come across these: Dispose from the .NET framework point-of-view: Implementing Finalize and Dispose to Clean Up Unmanaged Resources[^] and specifically for Managed C++: Destructors and Finalizers in Visual C++[^] Cheers, Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: