iPlug2 icon indicating copy to clipboard operation
iPlug2 copied to clipboard

IGRAPHICS_NANOVG/IGRAPHICS_FREETYPE crash with multiple plug-in instances

Open olilarkin opened this issue 4 years ago • 4 comments
trafficstars

Describe the bug

Crash with multiple instances of plug-ins when using nanovg with freetype, observed on Windows 10 with Live and Reaper. Thinking that fontstash or freetype or our code around nanovg text is not right for plugin context.

To Reproduce

  • set IGRAPHICS_FREETYPE in IPlugEffect
diff --git a/Examples/IPlugEffect/config/IPlugEffect-win.props b/Examples/IPlugEffect/config/IPlugEffect-win.props
index fdac335b6..f2141dbb7 100644
--- a/Examples/IPlugEffect/config/IPlugEffect-win.props
+++ b/Examples/IPlugEffect/config/IPlugEffect-win.props
@@ -3,7 +3,7 @@
   <PropertyGroup Label="UserMacros">
     <IPLUG2_ROOT>$(ProjectDir)..\..\..</IPLUG2_ROOT>
     <BINARY_NAME>IPlugEffect</BINARY_NAME>
-    <EXTRA_ALL_DEFS>IGRAPHICS_NANOVG;IGRAPHICS_GL2</EXTRA_ALL_DEFS>
+    <EXTRA_ALL_DEFS>IGRAPHICS_NANOVG;IGRAPHICS_GL2;IGRAPHICS_FREETYPE</EXTRA_ALL_DEFS>
     <EXTRA_DEBUG_DEFS />
     <EXTRA_RELEASE_DEFS />
     <EXTRA_TRACER_DEFS />

  • Build the VST3 project..
  • Insert multiple instances of IPlugEffectVST3 in Live, view the GUIs for each instance
  • Sometimes on closing an instance or closing the DAW - CRASH...
 	IPlugEffect.vst3!FT_Done_Library(FT_LibraryRec_ * library) Line 5395	C
 	IPlugEffect.vst3!FT_Done_FreeType(FT_LibraryRec_ * library) Line 249	C
>	IPlugEffect.vst3!fons__tt_done(FONScontext * context) Line 174	C++
 	IPlugEffect.vst3!fonsDeleteInternal(FONScontext * stash) Line 1653	C++
 	IPlugEffect.vst3!nvgDeleteInternal(NVGcontext * ctx) Line 353	C++
 	IPlugEffect.vst3!nvgDeleteGL2(NVGcontext * ctx) Line 1619	C++
 	IPlugEffect.vst3!iplug::igraphics::IGraphicsNanoVG::OnViewDestroyed() Line 477	C++
 	IPlugEffect.vst3!iplug::igraphics::IGraphicsWin::CloseWindow() Line 1278	C++
 	IPlugEffect.vst3!iplug::igraphics::IGEditorDelegate::CloseWindow() Line 55	C++
 	IPlugEffect.vst3!IPlugVST3View<iplug::IPlugVST3>::removed() Line 132	C++

To Reproduce another version

  • Build the VST3 project..
  • Insert multiple instances of IPlugEffect VST3 in Reaper
  • When inserting 2nd visible instance
 	IPlugEffect.vst3!FT_Request_Size(FT_FaceRec_ * face, FT_Size_RequestRec_ * req) Line 3273	C
 	IPlugEffect.vst3!FT_Set_Pixel_Sizes(FT_FaceRec_ * face, unsigned int pixel_width, unsigned int pixel_height) Line 3405	C
>	IPlugEffect.vst3!fons__tt_buildGlyphBitmap(FONSttFontImpl * font, int glyph, float size, float scale, int * advance, int * lsb, int * x0, int * y0, int * x1, int * y1) Line 213	C++
 	IPlugEffect.vst3!fons__getGlyph(FONScontext * stash, FONSfont * font, unsigned int codepoint, short isize, short iblur, int bitmapOption) Line 1110	C++
 	IPlugEffect.vst3!fonsTextBounds(FONScontext * stash, float x, float y, const char * str, const char * end, float * bounds) Line 1527	C++
 	IPlugEffect.vst3!nvgTextBounds(NVGcontext * ctx, float x, float y, const char * string, const char * end, float * bounds) Line 2813	C++
 	IPlugEffect.vst3!iplug::igraphics::IGraphicsNanoVG::PrepareAndMeasureText(const iplug::igraphics::IText & text, const char * str, iplug::igraphics::IRECT & r, double & x, double & y) Line 649	C++
 	IPlugEffect.vst3!iplug::igraphics::IGraphicsNanoVG::DoDrawText(const iplug::igraphics::IText & text, const char * str, const iplug::igraphics::IRECT & bounds, const iplug::igraphics::IBlend * pBlend) Line 668	C++

Expected behaviour No crashes

Additional context Not yet tested with other plug-in formats, platforms

olilarkin avatar Feb 20 '21 14:02 olilarkin

Similar behaviour on macOS

#0	0x0000000122135ede in ft_mem_free [inlined] at /Users/oli/Dev/iPlug2/Dependencies/Build/src/freetype/src/base/ftutil.c:173
#1	0x0000000122135edb in FT_Done_Library at /Users/oli/Dev/iPlug2/Dependencies/Build/src/freetype/src/base/ftobjs.c:5446
#2	0x000000012212dd73 in FT_Done_FreeType at /Users/oli/Dev/iPlug2/Dependencies/Build/src/freetype/src/base/ftinit.c:246
#3	0x0000000122058fc8 in fons__tt_done at /Users/oli/Dev/iPlug2/Dependencies/IGraphics/NanoVG/src/fontstash.h:174
#4	0x0000000122059b05 in fonsDeleteInternal at /Users/oli/Dev/iPlug2/Dependencies/IGraphics/NanoVG/src/fontstash.h:1652
#5	0x000000012205016f in nvgDeleteInternal at /Users/oli/Dev/iPlug2/Dependencies/IGraphics/NanoVG/src/nanovg.c:351
#6	0x0000000122050225 in nvgDeleteMTL at /Users/oli/Dev/iPlug2/Dependencies/IGraphics/MetalNanoVG/src/nanovg_mtl.m:493
#7	0x0000000121ff8805 in iplug::igraphics::IGraphicsNanoVG::OnViewDestroyed() at /Users/oli/Dev/iPlug2/IGraphics/Drawing/IGraphicsNanoVG.cpp:475
#8	0x0000000121ff8031 in iplug::igraphics::IGraphicsMac::CloseWindow() at /Users/oli/Dev/iPlug2/IGraphics/Platforms/IGraphicsMac.mm:153
#9	0x000000012211be65 in iplug::igraphics::IGEditorDelegate::CloseWindow() at /Users/oli/Dev/iPlug2/IGraphics/IGraphicsEditorDelegate.cpp:54
#10	0x000000012212852d in IPlugVST3View<iplug::IPlugVST3>::removed() at /Users/oli/Dev/iPlug2/IPlug/VST3/IPlugVST3_View.h:130

olilarkin avatar Feb 20 '21 22:02 olilarkin

Pretty sure that the Issue is in font stash.h line 160:

static FT_Library ftLibrary;

So ftLibrary is static and gets released twice. Unfortunately, this appears to be in how NanoVG interacts with fontstash/freetype, so on a first look it seems that nanovg might not play well with multiple contexts and freetype.

AlexHarker avatar Mar 28 '21 19:03 AlexHarker

Confirmed here: https://github.com/memononen/nanovg/issues/610

AlexHarker avatar Mar 28 '21 19:03 AlexHarker

There's a fix for this on NanoVG now, but it will involve us fixing the text sizes.

AlexHarker avatar Jan 15 '22 16:01 AlexHarker