NanoVG Crash

I’m working in a GUI system for my game but NanoVG is crashing the 80% of the time and don’t working correctly the 99% of the time.
The crashes goes from JVM Fatal Errors to JVM stops working, the crashes are fully randomly. One problem is that the font isn’t rendering. I am using the exact same code from the demo if is the case that my code has a bug but still has the same problem.
This are the JVM Fatal Error log (short log): http://pastebin.java-gaming.org/102c154933915

EDIT:
Some images of the errors:
Working good:

http://puu.sh/m7LQY/a4cf423366.png

Buged:

http://puu.sh/m7LOF/7aa9fb3c4f.png

Please post the contents of the crash log; must be in hs_err_pid1504.log

Here is: http://pastebin.java-gaming.org/02c146399351d

The log says that it crashed in your [icode]renderButton[/icode] method of [icode]MenuRendering[/icode] class, where when you call [icode]nvgTextBounds()[/icode] method.

I haven’t looked into NanoVG still, but can you post that code?

Here is:

	public static void renderButton(ByteBuffer preicon, String text, String font, float x, float y, float w, float h,
			NVGColor color, boolean mouseInside) {
		long vg = Display.getVg();
		NVGPaint bg = paintA;
		float cornerRadius = 4.0f;
		float tw, iw = 0;

		if (mouseInside) {
			x += 3;
			y += 3;
			w -= 6;
			h -= 6;
		}
		float fontSize = h / 2;

		nvgLinearGradient(vg, x, y, x, y + h, rgba(255, 255, 255, isBlack(color) ? 16 : 32, colorB),
				rgba(0, 0, 0, isBlack(color) ? 16 : 32, colorC), bg);
		nvgBeginPath(vg);
		nvgRoundedRect(vg, x + 1, y + 1, w - 2, h - 2, cornerRadius - 1);
		if (!isBlack(color)) {
			nvgFillColor(vg, color);
			nvgFill(vg);
		}
		nvgFillPaint(vg, bg);
		nvgFill(vg);

		nvgBeginPath(vg);
		nvgRoundedRect(vg, x + 0.5f, y + 0.5f, w - 1, h - 1, cornerRadius - 0.5f);
		nvgStrokeColor(vg, rgba(0, 0, 0, 48, colorA));
		nvgStroke(vg);

		ByteBuffer textEncoded = memEncodeASCII(text, BufferAllocator.MALLOC);

		nvgFontSize(vg, fontSize);
		nvgFontFace(vg, font);
		tw = nvgTextBounds(vg, 0, 0, textEncoded, NULL, (ByteBuffer) null);
		if (preicon != null) {
			nvgFontSize(vg, h * 1.3f);
			nvgFontFace(vg, "icons");
			iw = nvgTextBounds(vg, 0, 0, preicon, NULL, (ByteBuffer) null);
			iw += h * 0.15f;
		}

		if (preicon != null) {
			nvgFontSize(vg, h * 1.3f);
			nvgFontFace(vg, "icons");
			nvgFillColor(vg, rgba(255, 255, 255, 96, colorA));
			nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
			nvgText(vg, x + w * 0.5f - tw * 0.5f - iw * 0.75f, y + h * 0.5f, preicon, NULL);
		}

		nvgFontSize(vg, fontSize);
		nvgFontFace(vg, font);
		nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
		nvgFillColor(vg, rgba(0, 0, 0, 160, colorA));
		nvgText(vg, x + w * 0.5f - tw * 0.5f + iw * 0.25f, y + h * 0.5f - 1, textEncoded, NULL);
		nvgFillColor(vg, rgba(255, 255, 255, 160, colorA));
		nvgText(vg, x + w * 0.5f - tw * 0.5f + iw * 0.25f, y + h * 0.5f, textEncoded, NULL);

		memFree(textEncoded);
	}

Did you use nvgCreateFontMem to create the font? One thing that’s not at all obvious in the NanoVG documentation is that the buffer you pass to that function must remain valid for as long as the font is used for rendering. Your code is crashing in nvgTextBounds because internally NanoVG tries to access the font data buffer.

I’ll add this to the NanoVG javadoc soon.

Yes, i use the nvgCreateFontMem, but is strange that the code is the same as the demo and is crashing.

EDIT: The problem was resolved with the help of NegativeZero on the irc channel: If the font’s ByteBuffers go out of context they will be garbage collected and will be overwritten at some point causing a crash or undefined behavior. Put your ByteBuffers in higher scope and you should be fine!

Hey, new to the forum here! I just registered to look for information on the same exact topic. I’ve been having a very similar problem. Seemingly at random moments (without changing window state by maximizing or moving window) the nanoVG text rendering will completely glitch out in unexpected ways. I’m having a different glitch from what your font rendering seems to do. On my computer, at random times the font will change from normal rendering:

to glitched rendering where either the entire text is stacked in one position and only the first or last character is drawn, as seen in the following screenshot:

I’m just using some modifications of the test code that was provided with nanoVG, you can see the demo FPS counter. So I’m not doing anything complicated. After 15 seconds to a minute, or some random amount of time (I can’t correlate it with anything I’m inputting as a user) the text rendering goes back to normal.

To me this indicates a different problem other than the font buffer becoming invalid. If that was the case, the program would just crash, instead of displaying this bug and returning to normal seemingly at random.

Separately, I also do get the following crash when I change focus away from the program and back:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x000000012126621c, pid=11483, tid=2571
#
# JRE version: Java(TM) SE Runtime Environment (8.0_60-b27) (build 1.8.0_60-b27)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.60-b23 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# C  [liblwjgl.dylib+0x4c21c]
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /Users/chrisdalke/Desktop/Orbital Space/dev/hs_err_pid11483.log
Compiled method (nm)   22471  416     n 0       org.lwjgl.nanovg.NanoVG::nnvgText (native)
 total in heap  [0x0000000109fd1d10,0x0000000109fd2090] = 896
 relocation     [0x0000000109fd1e38,0x0000000109fd1e78] = 64
 main code      [0x0000000109fd1e80,0x0000000109fd2088] = 520
 oops           [0x0000000109fd2088,0x0000000109fd2090] = 8
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

this crash dump is likely what Spasi was talking about where the buffer is invalidated, so this isn’t as much an issue. I haven’t implemented reloading when focus is regained in my game yet so this crash is expected. I plan to continue using nanoVG for vector drawing so I’ll be watching this thread and helping to gather information.

Thanks! - Chris

EDIT: The problem was resolved with the help of NegativeZero on the irc channel: If the font’s ByteBuffers go out of context they will be garbage collected and will be overwritten at some point causing a crash or undefined behavior. Put your ByteBuffers in higher scope and you should be fine!

Now is fixed, storing the ByteBuffer of the font in a list and is working correctly, so thanks for the help and i hope if someone has the same issue read this.