Skip to content

Commit

Permalink
Fixed issue with delegates being garbage collected
Browse files Browse the repository at this point in the history
  • Loading branch information
dcronqvist committed Jun 2, 2023
1 parent 6b7470f commit 0692ee5
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 42 deletions.
2 changes: 1 addition & 1 deletion DotGLFW.Example/DotGLFW.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DotGLFW" Version="1.0.0" />
<PackageReference Include="DotGLFW" Version="1.0.1" />
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions DotGLFW.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ static void Main(string[] args)
glClearColor = Marshal.GetDelegateForFunctionPointer<glClearColorHandler>(Glfw.GetProcAddress("glClearColor"));
glClear = Marshal.GetDelegateForFunctionPointer<glClearHandler>(Glfw.GetProcAddress("glClear"));

Glfw.SetKeyCallback(window, (window, key, scancode, action, mods) =>
{
Console.WriteLine($"Key: {key}, Scancode: {scancode}, Action: {action}, Mods: {mods}");
});

GC.Collect();

while (!Glfw.WindowShouldClose(window))
{
Glfw.PollEvents();
Expand Down
120 changes: 80 additions & 40 deletions DotGLFW/GLFW/Glfw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ public static GlfwErrorCallback SetErrorCallback(GlfwErrorCallback callback)
return oldCallback;
}

NativeGlfw.SetErrorCallback((int errorCode, IntPtr description) =>
NativeGlfw._currentNativeGlfwErrorCallback = (int errorCode, IntPtr description) =>
{
_errorCallback?.Invoke((ErrorCode)errorCode, Marshal.PtrToStringUTF8(description));
});
};

NativeGlfw.SetErrorCallback(NativeGlfw._currentNativeGlfwErrorCallback);

return oldCallback;
}
Expand Down Expand Up @@ -279,11 +281,13 @@ public static GlfwMonitorCallback SetMonitorCallback(GlfwMonitorCallback callbac
return old;
}

NativeGlfw.SetMonitorCallback((IntPtr monitor, int @event) =>
NativeGlfw._currentNativeGlfwMonitorCallback = (IntPtr monitor, int @event) =>
{
Monitor m = Marshal.PtrToStructure<Monitor>(monitor);
callback(m, (ConnectionState)@event);
});
};

NativeGlfw.SetMonitorCallback(NativeGlfw._currentNativeGlfwMonitorCallback);

return old;
}
Expand Down Expand Up @@ -819,11 +823,13 @@ public static GlfwWindowPosCallback SetWindowPosCallback(Window window, GlfwWind
return old;
}

NativeGlfw.SetWindowPosCallback(window._handle, (IntPtr ptr, int x, int y) =>
NativeGlfw._currentNativeGlfwWindowPosCallback = (IntPtr ptr, int x, int y) =>
{
Window w = new Window(ptr);
_windowPosCallback(w, x, y);
});
};

NativeGlfw.SetWindowPosCallback(window._handle, NativeGlfw._currentNativeGlfwWindowPosCallback);

return old;
}
Expand All @@ -848,11 +854,13 @@ public static GlfwWindowSizeCallback SetWindowSizeCallback(Window window, GlfwWi
return old;
}

NativeGlfw.SetWindowSizeCallback(window._handle, (IntPtr ptr, int width, int height) =>
NativeGlfw._currentNativeGlfwWindowSizeCallback = (IntPtr ptr, int width, int height) =>
{
Window w = new Window(ptr);
_windowSizeCallback(w, width, height);
});
};

NativeGlfw.SetWindowSizeCallback(window._handle, NativeGlfw._currentNativeGlfwWindowSizeCallback);

return old;
}
Expand All @@ -877,11 +885,13 @@ public static GlfwWindowCloseCallback SetWindowCloseCallback(Window window, Glfw
return old;
}

NativeGlfw.SetWindowCloseCallback(window._handle, (IntPtr ptr) =>
NativeGlfw._currentNativeGlfwWindowCloseCallback = (IntPtr ptr) =>
{
Window w = new Window(ptr);
_windowCloseCallback(w);
});
};

NativeGlfw.SetWindowCloseCallback(window._handle, NativeGlfw._currentNativeGlfwWindowCloseCallback);

return old;
}
Expand All @@ -906,11 +916,13 @@ public static GlfwWindowRefreshCallback SetWindowRefreshCallback(Window window,
return old;
}

NativeGlfw.SetWindowRefreshCallback(window._handle, (IntPtr ptr) =>
NativeGlfw._currentNativeGlfwWindowRefreshCallback = (IntPtr ptr) =>
{
Window w = new Window(ptr);
_windowRefreshCallback(w);
});
};

NativeGlfw.SetWindowRefreshCallback(window._handle, NativeGlfw._currentNativeGlfwWindowRefreshCallback);

return old;
}
Expand All @@ -935,11 +947,13 @@ public static GlfwWindowFocusCallback SetWindowFocusCallback(Window window, Glfw
return old;
}

NativeGlfw.SetWindowFocusCallback(window._handle, (IntPtr ptr, int focused) =>
NativeGlfw._currentNativeGlfwWindowFocusCallback = (IntPtr ptr, int focused) =>
{
Window w = new Window(ptr);
_windowFocusCallback(w, focused != 0);
});
};

NativeGlfw.SetWindowFocusCallback(window._handle, NativeGlfw._currentNativeGlfwWindowFocusCallback);

return old;
}
Expand All @@ -964,11 +978,13 @@ public static GlfwWindowIconifyCallback SetWindowIconifyCallback(Window window,
return old;
}

NativeGlfw.SetWindowIconifyCallback(window._handle, (IntPtr ptr, int iconified) =>
NativeGlfw._currentNativeGlfwWindowIconifyCallback = (IntPtr ptr, int iconified) =>
{
Window w = new Window(ptr);
_windowIconifyCallback(w, iconified != 0);
});
};

NativeGlfw.SetWindowIconifyCallback(window._handle, NativeGlfw._currentNativeGlfwWindowIconifyCallback);

return old;
}
Expand All @@ -993,11 +1009,13 @@ public static GlfwWindowMaximizeCallback SetWindowMaximizeCallback(Window window
return old;
}

NativeGlfw.SetWindowMaximizeCallback(window._handle, (IntPtr ptr, int maximized) =>
NativeGlfw._currentNativeGlfwWindowMaximizeCallback = (IntPtr ptr, int maximized) =>
{
Window w = new Window(ptr);
_windowMaximizeCallback(w, maximized != 0);
});
};

NativeGlfw.SetWindowMaximizeCallback(window._handle, NativeGlfw._currentNativeGlfwWindowMaximizeCallback);

return old;
}
Expand All @@ -1022,11 +1040,13 @@ public static GlfwFramebufferSizeCallback SetFramebufferSizeCallback(Window wind
return old;
}

NativeGlfw.SetFramebufferSizeCallback(window._handle, (IntPtr ptr, int width, int height) =>
NativeGlfw._currentNativeGlfwFramebufferSizeCallback = (IntPtr ptr, int width, int height) =>
{
Window w = new Window(ptr);
_framebufferSizeCallback(w, width, height);
});
};

NativeGlfw.SetFramebufferSizeCallback(window._handle, NativeGlfw._currentNativeGlfwFramebufferSizeCallback);

return old;
}
Expand All @@ -1051,11 +1071,13 @@ public static GlfwWindowContentScaleCallback SetWindowContentScaleCallback(Windo
return old;
}

NativeGlfw.SetWindowContentScaleCallback(window._handle, (IntPtr ptr, float xScale, float yScale) =>
NativeGlfw._currentNativeGlfwWindowContentScaleCallback = (IntPtr ptr, float xScale, float yScale) =>
{
Window w = new Window(ptr);
_windowContentScaleCallback(w, xScale, yScale);
});
};

NativeGlfw.SetWindowContentScaleCallback(window._handle, NativeGlfw._currentNativeGlfwWindowContentScaleCallback);

return old;
}
Expand Down Expand Up @@ -1289,11 +1311,13 @@ public static GlfwKeyCallback SetKeyCallback(Window window, GlfwKeyCallback call
return old;
}

NativeGlfw.SetKeyCallback(window._handle, (IntPtr ptr, int key, int scancode, int action, int modifiers) =>
NativeGlfw._currentNativeGlfwKeyCallback = (IntPtr ptr, int key, int scancode, int action, int modifiers) =>
{
Window w = new Window(ptr);
callback(w, (Keys)key, scancode, (InputState)action, (ModifierKeys)modifiers);
});
};

NativeGlfw.SetKeyCallback(window._handle, NativeGlfw._currentNativeGlfwKeyCallback);

return old;
}
Expand All @@ -1318,11 +1342,13 @@ public static GlfwCharCallback SetCharCallback(Window window, GlfwCharCallback c
return old;
}

NativeGlfw.SetCharCallback(window._handle, (IntPtr ptr, uint codepoint) =>
NativeGlfw._currentNativeGlfwCharCallback = (IntPtr ptr, uint codepoint) =>
{
Window w = new Window(ptr);
callback(w, codepoint);
});
};

NativeGlfw.SetCharCallback(window._handle, NativeGlfw._currentNativeGlfwCharCallback);

return old;
}
Expand All @@ -1347,11 +1373,13 @@ public static GlfwCharModsCallback SetCharModsCallback(Window window, GlfwCharMo
return old;
}

NativeGlfw.SetCharModsCallback(window._handle, (IntPtr ptr, uint codepoint, int mods) =>
NativeGlfw._currentNativeGlfwCharModsCallback = (IntPtr ptr, uint codepoint, int mods) =>
{
Window w = new Window(ptr);
callback(w, codepoint, (ModifierKeys)mods);
});
};

NativeGlfw.SetCharModsCallback(window._handle, NativeGlfw._currentNativeGlfwCharModsCallback);

return old;
}
Expand All @@ -1376,11 +1404,13 @@ public static GlfwMouseButtonCallback SetMouseButtonCallback(Window window, Glfw
return old;
}

NativeGlfw.SetMouseButtonCallback(window._handle, (IntPtr ptr, int button, int action, int mods) =>
NativeGlfw._currentNativeGlfwMouseButtonCallback = (IntPtr ptr, int button, int action, int mods) =>
{
Window w = new Window(ptr);
callback(w, (MouseButton)button, (InputState)action, (ModifierKeys)mods);
});
};

NativeGlfw.SetMouseButtonCallback(window._handle, NativeGlfw._currentNativeGlfwMouseButtonCallback);

return old;
}
Expand All @@ -1405,11 +1435,13 @@ public static GlfwCursorPosCallback SetCursorPosCallback(Window window, GlfwCurs
return old;
}

NativeGlfw.SetCursorPosCallback(window._handle, (IntPtr ptr, double x, double y) =>
NativeGlfw._currentNativeGlfwCursorPosCallback = (IntPtr ptr, double x, double y) =>
{
Window w = new Window(ptr);
callback(w, x, y);
});
};

NativeGlfw.SetCursorPosCallback(window._handle, NativeGlfw._currentNativeGlfwCursorPosCallback);

return old;
}
Expand All @@ -1434,11 +1466,13 @@ public static GlfwCursorEnterCallback SetCursorEnterCallback(Window window, Glfw
return old;
}

NativeGlfw.SetCursorEnterCallback(window._handle, (IntPtr ptr, int entered) =>
NativeGlfw._currentNativeGlfwCursorEnterCallback = (IntPtr ptr, int entered) =>
{
Window w = new Window(ptr);
callback(w, entered != 0);
});
};

NativeGlfw.SetCursorEnterCallback(window._handle, NativeGlfw._currentNativeGlfwCursorEnterCallback);

return old;
}
Expand All @@ -1463,11 +1497,13 @@ public static GlfwScrollCallback SetScrollCallback(Window window, GlfwScrollCall
return old;
}

NativeGlfw.SetScrollCallback(window._handle, (IntPtr ptr, double x, double y) =>
NativeGlfw._currentNativeGlfwScrollCallback = (IntPtr ptr, double x, double y) =>
{
Window w = new Window(ptr);
callback(w, x, y);
});
};

NativeGlfw.SetScrollCallback(window._handle, NativeGlfw._currentNativeGlfwScrollCallback);

return old;
}
Expand All @@ -1492,7 +1528,7 @@ public static GlfwDropCallback SetDropCallback(Window window, GlfwDropCallback c
return old;
}

NativeGlfw.SetDropCallback(window._handle, (IntPtr ptr, int count, IntPtr names) =>
NativeGlfw._currentNativeGlfwDropCallback = (IntPtr ptr, int count, IntPtr names) =>
{
Window w = new Window(ptr);
Expand All @@ -1504,7 +1540,9 @@ public static GlfwDropCallback SetDropCallback(Window window, GlfwDropCallback c
}
callback(w, fileNames);
});
};

NativeGlfw.SetDropCallback(window._handle, NativeGlfw._currentNativeGlfwDropCallback);

return old;
}
Expand Down Expand Up @@ -1660,10 +1698,12 @@ public static GlfwJoystickCallback SetJoystickCallback(GlfwJoystickCallback call
return old;
}

NativeGlfw.SetJoystickCallback((int jid, int @event) =>
NativeGlfw._currentNativeGlfwJoystickCallback = (int jid, int @event) =>
{
callback((Joystick)jid, (ConnectionState)@event);
});
};

NativeGlfw.SetJoystickCallback(NativeGlfw._currentNativeGlfwJoystickCallback);

return old;
}
Expand Down
Loading

0 comments on commit 0692ee5

Please sign in to comment.