Memory leak in nuklear nk_edit_string
LordMonoxide opened this issue · 3 comments
Version
3.3.3
Platform
Windows x64
JDK
Coretto 17/21, Oracle 15/16/17, I assume all
Module
Nuklear
Bug description
Calling nk_edit_string
leaks memory due to memAddressSafe
allocating a callback. Java process memory will grow indefinitely (system memory, not jvm heap). I ran it with Configuration.DEBUG_MEMORY_ALLOCATOR.set(true)
and after leaving it running for a few minutes, when I shut it down the console is flooded with "bytes leaked" warnings.
Example code:
nk_edit_string(manager.ctx, NK_EDIT_FIELD | NK_EDIT_SIG_ENTER, <textBuffer>, <textLength>, maxLength + 1, Nuklear::nnk_filter_decimal)
Setting the last param (filter
) to null prevents the callback instance from being allocated and no memory is leaked.
Stacktrace or crash log output
[LWJGL] 56 bytes leaked, thread 1 (main), address: 0x19B92376C50
at org.lwjgl.system.Callback.create(Callback.java:163)
at org.lwjgl.system.CallbackI.address(CallbackI.java:25)
at org.lwjgl.system.MemoryUtil.memAddressSafe(MemoryUtil.java:855)
at org.lwjgl.nuklear.Nuklear.nk_edit_string(Nuklear.java:5000)
at org.legendofdragoon.fonteditor.opengl.Gui.numberbox(Gui.java:128)
at org.legendofdragoon.fonteditor.opengl.FontEditorGui.lambda$draw$1(FontEditorGui.java:160)
at org.legendofdragoon.fonteditor.opengl.Gui.row(Gui.java:99)
at org.legendofdragoon.fonteditor.opengl.FontEditorGui.lambda$draw$9(FontEditorGui.java:145)
at org.legendofdragoon.fonteditor.opengl.Gui.simpleWindow(Gui.java:85)
at org.legendofdragoon.fonteditor.opengl.FontEditorGui.draw(FontEditorGui.java:135)
at org.legendofdragoon.fonteditor.opengl.Gui.draw(Gui.java:55)
at org.legendofdragoon.fonteditor.opengl.GuiManager.draw(GuiManager.java:409)
at org.legendofdragoon.fonteditor.FontEditor.lambda$new$4(FontEditor.java:82)
at org.legendofdragoon.fonteditor.opengl.Context.draw(Context.java:96)
at org.legendofdragoon.fonteditor.opengl.Window$Events.onDraw(Window.java:337)
at org.legendofdragoon.fonteditor.opengl.Window.run(Window.java:216)
at org.legendofdragoon.fonteditor.FontEditor.<init>(FontEditor.java:87)
at org.legendofdragoon.fonteditor.Main.main(Main.java:29)
I was able to temporarily work around this issue by caching the result of memAddressSafe((NkPluginFilterI)Nuklear::nnk_filter_decimal)
and making my own nk_edit_string
that accepts a long for the filter param
AFAIK this is a side-effect of the API design here, see https://github.com/LWJGL/lwjgl3-wiki/wiki/1.4.-Bindings-FAQ#how-can-callbacks-from-native-code-be-configured. This wiki page also includes patterns for removing the leak.
I see, thanks for the information. I feel that using method references like this should be prevented by the API, but don't have any suggestions for that.