styluslabs / nanovgXC

Lightweight vector graphics library implementing exact-coverage antialiasing in OpenGL
Other
139 stars 6 forks source link

memory leak problem #7

Closed hellozhangchi closed 2 weeks ago

hellozhangchi commented 11 months ago

I have a drawing plugin that loads every time I need to draw, and unloads after drawing. NVGcontext keeps on creating and destroying, which in this case triggers a serious memory leak. You can test this with the following code `while (!glfwWindowShouldClose(window)) { vg = nvglCreate(NVG_SRGB); //NVG_DEBUG); double mx, my, t, dt; int winWidth, winHeight; int fbWidth, fbHeight; float pxRatio; int prevFBO; float gpuTimes[3]; int i, n;

t = glfwGetTime();
dt = t - prevt;
prevt = t;

startGPUTimer(&gpuTimer);

glfwGetCursorPos(window, &mx, &my);
glfwGetWindowSize(window, &winWidth, &winHeight);
glfwGetFramebufferSize(window, &fbWidth, &fbHeight);
// Calculate pixel ration for hi-dpi devices.
pxRatio = (float)fbWidth / (float)winWidth;

// Update and render
nvgluSetViewport(0, 0, fbWidth, fbHeight);
nvgluClear(nvgRGBAf(0.3f, 0.3f, 0.32f, 1.0f));

nvgBeginFrame(vg, winWidth, winHeight, pxRatio);

nvgLineCap(vg, NVG_BUTT);
nvgLineJoin(vg, NVG_ROUND);

nvgStrokeWidth(vg, 5);
nvgStrokeColor(vg, nvgRGBA(255, 0, 0, 160));
nvgBeginPath(vg);
nvgMoveTo(vg, 100, 100);
nvgLineTo(vg, 300, 100);
nvgLineTo(vg, 300, 500);
nvgLineTo(vg, 700, 700);
float dashes[]{ 5.0f, -1.0f };
nvgDashArray(vg, dashes);
nvgStroke(vg);

nvgEndFrame(vg);

glfwSwapBuffers(window);
glfwPollEvents();
nvglDelete(vg);

}`

pbsurf commented 10 months ago

Can you try the latest version (updated today) with valgrind to try to find the leak? I was not able to find any leaks in nanovgXC itself with valgrind memcheck.

Also, since the shaders are compiled every time NVGcontext is created, it would be better to reuse the same NVGcontext if possible.