ericfreese/node-freetype2

segfault when trying to access glyph bitmaps

sidorares opened this issue · 2 comments

when I run following example

var fs = require('fs');
var ft2 = require('freetype2');
var fontface= ft2.New_Memory_Face(fs.readFileSync(process.argv[2]), 0); // 0: index of the face. font might hold multiple faces
var getAvailableCharacters = function(face) {
  var gindex = {},
      charcode,
      chars = [];

  charcode = ft2.Get_First_Char(face, gindex);
  while (gindex.gindex !== 0) {
    chars.push(charcode);
    charcode = ft2.Get_Next_Char(face, charcode, gindex);
  }

  return chars;
}

var chars = getAvailableCharacters(fontface);

ft2.Set_Char_Size(fontface, 0, 40 * 64, 72, 0);
var bitmaps = chars.slice(20, 30).map(function(ch) {
  var i = ft2.Get_Char_Index(fontface, ch);
  ft2.Load_Char(fontface, i, ft2.LOAD_RENDER);
  var g = fontface.glyph;
  //console.log(g);
  return fontface.glyph.bitmap.buffer;
});

console.log(bitmaps);

I'm getting

node(10417,0x7fff7a04f300) malloc: *** error for object 0x1025002e0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug

looks like font does not matter, I used Arial and Times ( from standard mac library - /Library/Fonts/Arial.ttf ). Node 4.1.0

Looks like this was caused by using Nan::NewBuffer instead of Nan::CopyBuffer. When using Nan::NewBuffer, Nan assumes ownership of the pointer and tries to free it when the Buffer is garbage collected. More info here.

Should be fixed with v0.2.3.

Thanks @ericfreese , works great!