Skip to content

Finalizers #148

@tomspilman

Description

@tomspilman

It would be nice for CppSharp to generate C# finalizers for all wrapped objects. I currently work around this by adding them to a separate source file as a partial class implementation:

    public unsafe partial class Texture
    {
        ~Texture()
        {
            Dispose(false);
        }
    }

    public unsafe partial class RenderTarget
    {
        ~RenderTarget()
        {
            Dispose(false);
        }
    }

The finalizer is necessary to properly cleanup the unmanaged parts of the wrapper when the user omits a direct call to Dispose(). You already call GC.SuppressFinalize(this) which means the finalizer will not do any extra work unless it is needed.

Some people prefer to not have finalizers and instead force the caller to properly dispose of their objects. In this case adding a finalizer could be optional and when disabled it could add something like this instead:

#if DEBUG
    ~WrapperClass()
    {
        Debug.Fail("You forgot to call Dispose!");
    }
#endif

To alert the user that they forgot to Dispose() this object and that the unmanaged objects were leaked.

By the way we're using CppSharp to generate C# wrappers for a project running on the PS4. It has been fantastic and much easier than if we needed to generate our own bindings.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions