flecs-hub / flecs-cs

Automatically updated C# bindings for flecs https://github.com/SanderMertens/flecs with native dynamic link libraries.
MIT License
60 stars 9 forks source link

Suggestion: add IDisposable to CString #119

Closed clibequilibrium closed 1 year ago

clibequilibrium commented 1 year ago

Hello,

The motivation behind implementing IDisposable is to allow automatic disposure of CString when they are no longer used by flecs

For example:

using (var cname = (Runtime.CString)name)
            {
                ecs_query_desc_t queryDescription = new()
                {
                    filter = new ecs_filter_desc_t { expr =  cname },
                };

                return ecs_query_init(World.Handle, &queryDescription);
            }
        }

Will automatically free the allocated CString once you leave the scope of using.

It is simple to implement in CString via

public readonly unsafe struct CString : IEquatable<CString>, IDisposable
public void Dispose()
 {
     if(!IsNull)
        Marshal.FreeHGlobal(_pointer);
}
lithiumtoast commented 1 year ago

Implemented in C2CS v5.3; https://github.com/bottlenoselabs/c2cs/commit/98f0cc592d493801c81f7118d2e3aa4574457b0c