Embedded Font Causes a Crash

前端 未结 1 1293
执念已碎
执念已碎 2020-12-07 04:46

I have a WinForm app. I am using a custom font that is in my embedded resources.
It works at first, but then causes the program to crash after a while.
Using the fol

相关标签:
1条回答
  • 2020-12-07 05:27
            Marshal.FreeCoTaskMem(FontPtr)
    

    The MSDN documentation for PrivateFontCollection is too obtuse about this. But you need to keep the memory for the added font valid until you can no longer use the font. Or to put it another way, AddMemoryFont() does not make a copy of the font. So your program will fall over with a mysterious GDI+ error when it tries to access the font data and it got overwritten by another unmanaged memory allocation.

    Move the FreeCoTaskMem() call to a FormClosed event handler. Or don't bother if closing the form also terminates your program.

    0 讨论(0)
提交回复
热议问题