.net - Embedded Font Causes a Crash -


i have winform app. using custom font in embedded resources.
works @ first, causes program crash after while.
using following code example, if keep resizing form, forcing redraw itself, crash within few seconds. message 'error in 'form1_paint()'. object in use elsewhere.'.
doing wrong? how can avoid this?
got font here.
thanks.

imports system.drawing.text imports system.runtime.interopservices  public class form1     friend harabara font      private sub form1_load(byval sender system.object, byval e system.eventargs) handles mybase.load         loadfonts()     end sub      private sub form1_paint(byval sender object, byval e system.windows.forms.painteventargs) handles me.paint         try             e.graphics.drawstring("this drawn using custom font 'harabara'", harabara, brushes.lime, 10.0f, 10.0f)         catch ex exception             msgbox("error in form1_paint()'" & vbcrlf & ex.message)         end try     end sub      public sub loadfonts()         try             harabara = getfontinstance(my.resources.harabarahand, 24.0f, fontstyle.italic)         catch ex exception             msgbox("error in 'loadfonts()'" & vbcrlf & ex.message)         end try     end sub      private function getfontinstance(byval data() byte, byval size single, byval style fontstyle) font         dim result font         try             dim pfc = new privatefontcollection             'load memory pointer font resource             dim fontptr system.intptr = marshal.alloccotaskmem(data.length)             'copy data memory location             marshal.copy(data, 0, fontptr, data.length)             'load memory font private font collection             pfc.addmemoryfont(fontptr, data.length)             'free unsafe memory             marshal.freecotaskmem(fontptr)              result = new font(pfc.families(0), size, style)             pfc.families(0).dispose()             pfc.dispose()         catch ex exception             'error loading font. handle exception here             msgbox("error in 'getfontinstance()'" & vbcrlf & ex.message)             result = new font(fontfamily.genericmonospace, 8)         end try         return result     end function end class 

        marshal.freecotaskmem(fontptr) 

the msdn documentation privatefontcollection obtuse this. need keep memory added font valid until can no longer use font. or put way, addmemoryfont() not make copy of font. program fall on mysterious gdi+ error when tries access font data , got overwritten unmanaged memory allocation.

move freecotaskmem() call formclosed event handler. or don't bother if closing form terminates program.


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -