haplokuon / netDxf

.net dxf Reader-Writer
MIT License
992 stars 401 forks source link

GDI+ Access violation caused by PrivateFontCollection in TextStyle.cs #106

Open pnjeffries opened 5 years ago

pnjeffries commented 5 years ago

When using netDXF to read a DXF file that includes TextStyle information in Unity, the application crashes due to an Access Violation in GdiPlus.dll GdipDeleteFontFamily(). This occurs in the built version of the Unity executable, but not when running in the editor. This appears to be caused by the use of PrivateFontCollection within TestStyle.cs TrueTypeFontFamilyName. Commenting out the section of code (lines 375 to 382) using PrivateFontCollection prevents the crash. I tried calling Dispose() on the PrivateFontCollection but this did not help.

haplokuon commented 5 years ago

I guess your problem is related with this. This is a user pull request merged to the master branch sometime ago, but still not included in the latest release.

pnjeffries commented 5 years ago

Yes, it's the same bit of code, but the try-catch doesn't help in this case because the CLR won't catch 'corrupted state exceptions' like access violations by default. One 'solution' would be to use the HandleProcessCorruptedStateExceptions attribute to force it to handle them, but this might still leave memory in a corrupted state, so probably not a good idea. A better fix might be to extract the font names from the TTF files directly without using PrivateFontCollection.

haplokuon commented 5 years ago

Well, if that was not your problem, I cannot help, I dont have dont use Unity.

haplokuon commented 5 years ago

With the latest commit [2019/05/21] I removed the need to use the PrivateFontCollection class from the constructor of the TextStyle. See this post.

13704017891 commented 5 years ago

@pnjeffries Hello, I am also using this library in unity, but I don't know how to draw the model, can you refer to your project? I need some ideas.Thank you so much