memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.15k stars 770 forks source link

Frequent JVM crashes #670

Closed Chryfi closed 6 months ago

Chryfi commented 6 months ago

I frequently get JVM crashes. Specs: Java 17, Windows 11, NanoVG 3.3.3 and LWJGL 3.3.1. They are sometimes random and never happen immediately after rendering. I create the NanoVGL3 context once and load my fonts to it like this int fontHandle = NanoVG.nvgCreateFontMem(this.vg, font.getIdentifier().toString(), buffer.flip(), false);

in the end I do this NanoVGGL3.nvgDelete(this.vg);

this is how I render

glEnable(GL_STENCIL_TEST);
int vertexArray = GLUtils.getVertexArrayBinding();
int activeTexture = GLUtils.getActiveTexture();
int currentProgram = GLUtils.getCurrentProgram();
int[] windowSize = GLUtils.getGLFWWindowSize(window);
int[] fbsize = GLUtils.getGLFWFrameBufferSize(window);
NanoVG.nvgBeginFrame(this.vg, windowSize[0], windowSize[1], (float) fbsize[0] / windowSize[0]);

NVGColor textColor = NVGColor.create();
textColor.r(color.getR());
textColor.g(color.getG());
textColor.b(color.getB());
textColor.a(color.getA());

this.setupNanoVGFont(font, fontSize, this.defaultAlignMode);
NanoVG.nvgFillColor(this.vg, textColor);
NanoVG.nvgText(this.vg, x, y, text);

NanoVG.nvgEndFrame(this.vg);
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
GL30.glBindVertexArray(vertexArray);
glActiveTexture(activeTexture);
glUseProgram(currentProgram);

Besides that the only methods I call in other places are

float[] bounds = new float[4];
float[] lineh = new float[1];
float[] asc = new float[1];
float[] desc = new float[1];

NanoVG.nvgFontFace(this.vg, font.toString());
NanoVG.nvgFontSize(this.vg, fontSize);
NanoVG.nvgTextAlign(this.vg, alignMode);
NanoVG.nvgTextBounds(this.vg, 0, 0, text, bounds);
NanoVG.nvgTextMetrics(this.vg, asc, desc, lineh);

here is the thread dump. hs_err_pid36308.log

Is this a bug or am I doing something wrong?

mulle-nat commented 6 months ago

Java is a garbage collected language. If the crashes are sort of unpredictable, my first theory would be that something has vanished, which shouldn't. You gotta be sure that all objects you are using, keep being referenced by someone else for the duration of the use by NanoVG and OpenGL. (For example: I would check that NanoVG.nvgFontFace(this.vg, font.toString()); does the right thing). Otherwise I have no idea.

I am just answering this, because no one else has, not because I have any deep insight

Chryfi commented 6 months ago

Thanks, it seems like it was the byetbuffer that I used for the font that got garbage collected. It was a mistake on my end.