Skip to content

Commit

Permalink
Fix leak when disposing query builder, move free logic to context str…
Browse files Browse the repository at this point in the history
…ucts
  • Loading branch information
BeanCheeseBurrito committed Nov 19, 2024
1 parent 91fbb01 commit 19c3324
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 92 deletions.
1 change: 0 additions & 1 deletion src/Flecs.NET.Codegen/Helpers/Type.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

using System.Diagnostics.CodeAnalysis;

namespace Flecs.NET.Codegen.Helpers;
Expand Down
32 changes: 19 additions & 13 deletions src/Flecs.NET/Core/BindingContext/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,54 @@ namespace Flecs.NET.Core.BindingContext;
/// </summary>
internal static unsafe class Functions
{
#region Context Free
#region Context Free

[UnmanagedCallersOnly]
internal static void WorldContextFree(WorldContext* context)
{
context->Dispose();
Memory.Free(context);
WorldContext.Free(context);
}

[UnmanagedCallersOnly]
internal static void IteratorContextFree(IteratorContext* context)
{
context->Dispose();
Memory.Free(context);
IteratorContext.Free(context);
}

[UnmanagedCallersOnly]
internal static void RunContextFree(RunContext* context)
{
context->Dispose();
Memory.Free(context);
RunContext.Free(context);
}

[UnmanagedCallersOnly]
internal static void QueryContextFree(QueryContext* context)
{
context->Dispose();
Memory.Free(context);
QueryContext.Free(context);
}

[UnmanagedCallersOnly]
internal static void GroupByContextFree(GroupByContext* context)
{
context->Dispose();
Memory.Free(context);
GroupByContext.Free(context);
}

[UnmanagedCallersOnly]
internal static void SystemContextFree(SystemContext* context)
{
SystemContext.Free(context);
}

[UnmanagedCallersOnly]
internal static void ObserverContextFree(ObserverContext* context)
{
ObserverContext.Free(context);
}

[UnmanagedCallersOnly]
internal static void TypeHooksContextFree(TypeHooksContext* context)
{
context->Dispose();
Memory.Free(context);
TypeHooksContext.Free(context);
}

internal static void UserContextFinishDelegate<T>(ref UserContext context)
Expand Down
17 changes: 16 additions & 1 deletion src/Flecs.NET/Core/BindingContext/GroupByContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal struct GroupByContext : IDisposable
internal unsafe struct GroupByContext : IDisposable
{
public Callback GroupBy;
public Callback GroupCreate;
Expand All @@ -16,4 +17,18 @@ public void Dispose()
GroupDelete.Dispose();
GroupByUserContext.Dispose();
}

public static void Free(GroupByContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref GroupByContext context)
{
fixed (GroupByContext* ptr = &context)
Free(ptr);
}
}
17 changes: 16 additions & 1 deletion src/Flecs.NET/Core/BindingContext/IteratorContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal struct IteratorContext : IDisposable
internal unsafe struct IteratorContext : IDisposable
{
public Callback Callback;

public void Dispose()
{
Callback.Dispose();
}

public static void Free(IteratorContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref IteratorContext context)
{
fixed (IteratorContext* ptr = &context)
Free(ptr);
}
}
28 changes: 28 additions & 0 deletions src/Flecs.NET/Core/BindingContext/ObserverContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal unsafe struct ObserverContext : IDisposable
{
public UserContext UserContext;

public void Dispose()
{
UserContext.Dispose();
}

public static void Free(ObserverContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref ObserverContext context)
{
fixed (ObserverContext* ptr = &context)
Free(ptr);
}
}
17 changes: 16 additions & 1 deletion src/Flecs.NET/Core/BindingContext/OsApiContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal struct OsApiContext : IDisposable
internal unsafe struct OsApiContext : IDisposable
{
public Callback Abort;
public Callback Log;
Expand All @@ -12,4 +13,18 @@ public void Dispose()
Abort.Dispose();
Log.Dispose();
}

public static void Free(OsApiContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref OsApiContext context)
{
fixed (OsApiContext* ptr = &context)
Free(ptr);
}
}
2 changes: 2 additions & 0 deletions src/Flecs.NET/Core/BindingContext/Pointers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ static Pointers()
internal static readonly nint RunContextFree = (nint)(delegate* unmanaged<RunContext*, void>)&Functions.RunContextFree;
internal static readonly nint QueryContextFree = (nint)(delegate* unmanaged<QueryContext*, void>)&Functions.QueryContextFree;
internal static readonly nint GroupByContextFree = (nint)(delegate* unmanaged<GroupByContext*, void>)&Functions.GroupByContextFree;
internal static readonly nint SystemContextFree = (nint)(delegate* unmanaged<SystemContext*, void>)&Functions.SystemContextFree;
internal static readonly nint ObserverContextFree = (nint)(delegate* unmanaged<ObserverContext*, void>)&Functions.ObserverContextFree;
internal static readonly nint TypeHooksContextFree = (nint)(delegate* unmanaged<TypeHooksContext*, void>)&Functions.TypeHooksContextFree;

#endregion
Expand Down
17 changes: 16 additions & 1 deletion src/Flecs.NET/Core/BindingContext/PostFrameContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal struct PostFrameContext : IDisposable
internal unsafe struct PostFrameContext : IDisposable
{
public Callback Callback;

public void Dispose()
{
Callback.Dispose();
}

public static void Free(PostFrameContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref PostFrameContext context)
{
fixed (PostFrameContext* ptr = &context)
Free(ptr);
}
}
16 changes: 15 additions & 1 deletion src/Flecs.NET/Core/BindingContext/QueryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Flecs.NET.Core.BindingContext;

internal struct QueryContext : IDisposable
internal unsafe struct QueryContext : IDisposable
{
public Callback OrderBy;
public Callback ContextFree;
Expand All @@ -24,4 +24,18 @@ public void Dispose()

Strings.Dispose();
}

public static void Free(QueryContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref QueryContext context)
{
fixed (QueryContext* ptr = &context)
Free(ptr);
}
}
17 changes: 16 additions & 1 deletion src/Flecs.NET/Core/BindingContext/RunContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal struct RunContext : IDisposable
internal unsafe struct RunContext : IDisposable
{
public Callback Callback;

public void Dispose()
{
Callback.Dispose();
}

public static void Free(RunContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref RunContext context)
{
fixed (RunContext* ptr = &context)
Free(ptr);
}
}
28 changes: 28 additions & 0 deletions src/Flecs.NET/Core/BindingContext/SystemContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal unsafe struct SystemContext : IDisposable
{
public UserContext UserContext;

public void Dispose()
{
UserContext.Dispose();
}

public static void Free(SystemContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref SystemContext context)
{
fixed (SystemContext* ptr = &context)
Free(ptr);
}
}
17 changes: 16 additions & 1 deletion src/Flecs.NET/Core/BindingContext/TypeHooksContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal struct TypeHooksContext : IDisposable
internal unsafe struct TypeHooksContext : IDisposable
{
public int Header;

Expand All @@ -28,4 +29,18 @@ public void Dispose()
OnRemove.Dispose();
ContextFree.Dispose();
}

public static void Free(TypeHooksContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref TypeHooksContext context)
{
fixed (TypeHooksContext* ptr = &context)
Free(ptr);
}
}
14 changes: 14 additions & 0 deletions src/Flecs.NET/Core/BindingContext/WorldContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,18 @@ public void Dispose()
foreach (nint ptr in WorldFinishContexts)
Memory.Free((WorldFinishContext*)ptr);
}

public static void Free(WorldContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref WorldContext context)
{
fixed (WorldContext* ptr = &context)
Free(ptr);
}
}
17 changes: 16 additions & 1 deletion src/Flecs.NET/Core/BindingContext/WorldFinishContext.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
using System;
using Flecs.NET.Utilities;

namespace Flecs.NET.Core.BindingContext;

internal struct WorldFinishContext : IDisposable
internal unsafe struct WorldFinishContext : IDisposable
{
public Callback Callback;

public void Dispose()
{
Callback.Dispose();
}

public static void Free(WorldFinishContext* context)
{
if (context == null)
return;
context->Dispose();
Memory.Free(context);
}

public static void Free(ref WorldFinishContext context)
{
fixed (WorldFinishContext* ptr = &context)
Free(ptr);
}
}
Loading

0 comments on commit 19c3324

Please sign in to comment.