Q:

freetype 2 library crashes when trying to access characters map

I'm trying to use this awesome version of freetype 2 with my multiplatform library. I didn't test it on Android yet, but I've tried to compile and test it under win32. It compiles just fine without errors, but it always crashes when library tries to read the "data" (which is always bad pointer) member of TT_CMap. The function wher it happens depends only of font file, i.e. one font - one place. I'vew tried different ttd fonts from Windows, MacOS X and from Android, but it affects only on position, where segmentation fault will occure. 
For example, if I try to retreive index of any chracter of standard window arial.ttf font, it will always crash here:
static FT_UInt tt_cmap4_char_map_binary( TT_CMap     cmap, FT_UInt32*  pcharcode, FT_Bool     next )
{
    [...]
    p = cmap->data + 6;
    num_segs2 = FT_PAD_FLOOR( TT_PEEK_USHORT( p ), 2 );
    [...]

because of cmap->data is invalid pointer.
My test code looks like this:
int error = 0;

// Initialize freetype library first
error = FT_Init_FreeType( &ft_lib );
if ( error ) {
    ODF(("Unable to init freetype library! Error code is %d.\n", error));
    return false;
}

// Load font file into the memory buffer
iMemBuff faceFileBuff;
if (igelLoadResource(iString("Data/arial.ttf"), faceFileBuff) != ROR_Success) {
    ODF(("ERROR: Unable to open font file!\n"));
    return false;
}

// Init font
error = FT_New_Memory_Face( ft_lib, (FT_Byte*)faceFileBuff.GetPtr(), faceFileBuff.GetSize(), 0, &ft_face );
if ( error == FT_Err_Unknown_File_Format ) {
    ODF(("ERROR: the font file could be opened and read, but it appears that its font format is unsupported.\n"));
    return false;
} else if ( error ) {
    ODF(("ERROR: %d error code means that the font file could not be opened or read, or simply that it is broken...\n", error));
    return false;
}

// Setup font metrics
error = FT_Set_Pixel_Sizes( ft_face, 0, 16 );
// error = FT_Set_Char_Size(ft_face, 0, 16*64, 300, 300 );
if ( error  ) {
    ODF(("ERROR: Unable to set char/pixel size! Error code %d.\n", error));
    return false;
}

sint32 ox = 0, oy = 0;
int error = 0;
FT_GlyphSlot  slot = ft_face->glyph;  // a small shortcut
for (uint32 cc=0; cc<text.StringSize(); ++cc) {

    // Find the character 'a' index
    int glyph_index = FT_Get_Char_Index( ft_face, text.CStr()[cc] );

    // Load glyph
    error = FT_Load_Glyph( ft_face, glyph_index, FT_LOAD_DEFAULT );
    if ( error  ) {
        ODF(("ERROR: Unable to load face from the font! Error code %d.\n", error));
        continue;
    }

    // Render the glyph
    error = FT_Render_Glyph( ft_face->glyph, FT_RENDER_MODE_NORMAL );
    if ( error  ) {
        ODF(("ERROR: Unable to render glyph! Error code %d.\n", error));
        continue;
    }

    // Draw the glyph to our target surface
    ComposeGlyph(image, &slot->bitmap, ox + slot->bitmap_left, oy + slot->bitmap_top);
    ox += slot->advance.x >> 6;
}

And it crashes when i call FT_Get_Char_Index with any character...
Any Idea?

A:

In my case, the problem is cmap->data becomes bad pointer when I release the font memory block passed to FT_New_Memory_Face(). cmap->data keeps valid as long as I keep the memory block.
Although this seems not your case, just want to give some input.

