CppSharp
CppSharp copied to clipboard
Generated classes with Dispose should generate finalizer
Brief Description
For the classes that generate a dispose method, they should also have a finalizer. Without a finalizer, the code can leak handles when it's collected by the gc without freeing the handle.
Proposed generation output;
~ClassName() => Dispose(false);
// Private if the class is sealed, protected virtual if it is not.
protected virtual void Dispose(bool disposing)
{
Marshal.FreeHGlobal(...);
if(disposing) GC.SurpressFinalize(this);
}
public void Dispose() => Dispose(true);
If I have some free time soon I'll put up a pr for this.
The GC.SupressFinalize(this) call will prevent the finalizer from running when disposed properly, otherwise the gc will run the finalizer and free the pointer. This will prevent a handle leak by always making sure the handle is freed.
(Non os specific, code generation)
OS: N/A