question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GLFW callbacks not released

See original GitHub issue

Summary

After closing a GLFW rendering window GLFW ande some referenced objects remain in memory.

Steps to reproduce

  • Platform: Desktop
  • Framework Version: .NET 5
  • API: OpenGL
  1. Run the code from ‘Tutorial 1.1 - Hello Window’ in the debugger with a breakpoint on the closing brace of Main.
  2. Once the breakpoint is hit create a memory snapshot and open it.
  3. Inspecting the snapshot shows that the callbacks created in GlfwWindow (e.g. WindowCloseCallback or FramebufferSizeCallback) are still in memory.

Comments

From some debugging I think this might be due to those callbacks being lambdas/closures referenced by GlfwWindow.

Also I am not sure if GlfwWindow.UnregisterCallbacks acutally unregisters all of them. At least GCUtility.Unpin does not find the corresponding handles. To me it looks like only the most recent one is actually pinned due using PinMode.UntilNextCall.

I could get rid of the objects locally by making two changes:

  1. In Glfw change all Callbacks from [PinObject(PinMode.UntilNextCall)] to [PinObject]
  2. In GlfwWindow.UnregisterCallbacks after calling Unpin also set the fields to null

Since I do not understand this code well enough I doubt this is the right thing to do. The first step was because I noticed that GCUtilities.Pins contained only a single handle at all which causes Unpin not to find the callbacks in there.

Background

I have a service for rendering 3D scenes to images. This creates a new window for each request and I found that the memory usage keeps growing over time. Doing some memory profiling brought up that those Glfw* objects remain in memory.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
Perkseycommented, Mar 7, 2022

To clarify: I’d prefer the slot to be per native entrypoint rather than per entire method, because what if we had two overloads of the same native function but with different delegate types? We still need to free the other one.

By the way, thanks for the engaging conversation! You seem to have caught onto all this really quickly, and your input is really helpful 😃

0reactions
Perkseycommented, Jan 11, 2023

The GC slots aren’t actually used anywhere other than the GC Utility to my knowledge, which accepts the integer as an arbitrary index to map to a list of GC handles. GCUtility.PinUntilNextCall will resolve all GC handles for the given slot.

Right now the slot is always 0. This is the bug, as it’s causing only one delegate to be pinned at any given time (well, it’s one of the bugs, the other is that GCUtility doesn’t do a null check and as a result doesn’t properly deallocate).

We need to define that slot value, and I don’t think it makes sense to give each individual overload of the same native function a slot. So, this presents us with two options:

  • Allocate a GC slot by mapping the native entry point to a slot using a dictionary (aka a hash map)
  • Let the native entry point be the slot

I could be missing something though!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Input guide
Callbacks are more work to use than polling but is less CPU intensive and guarantees that you do not miss state changes. All...
Read more >
glfwSetKeyCallback() from GLFW is not called constantly ...
Just use glfwGetKey(window,key) to get the current key status (this returns GLFW_PRESS or GLFW_RELEASE ). See here for more details.
Read more >
GLFW callbacks in main.cpp : r/opengl
I find it a little bit messy so I was wondering if anyone has any suggestions on how I can organize the GLFW...
Read more >
Modifying GLFW callbacks
I'm currently using Imgui for the GUI part of my OpenGL/C++ engine with the GLFW binding. The problem is though that this binding...
Read more >
derelict glfw won't set callbacks - dlang forum
Any attempt to set callbacks in GLFW returns a null and the callback doesn't work. The first enforcement fails in this example: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found