I have just been learning about finalizers in C++/CLI and saw quite a bit of sample code that duplicated the same resource-freeing code in both the destructor and finalizer. It seems obvious to me that I should put the resource-freeing code in a single place -- another private member function (see sample code at the end of this message).
So I have to questions:
- Am I doing the correct thing in this sample code Am I missing any important concept
- Is there a recommended standard name for the C++/CLI member function to free resources
Many thanks for your help!
- Kevin Hall
ref class Resource
{
private:
// Is there a recommended standard
// name for this member function
void FreeResources()
{
Console::WriteLine(L"Freeing resources...");
}
public:
virtual ~Resource() // IDisposable
{
FreeResources();
}
protected:
!Resource() // Finalize
{
FreeResources();
}
};