freetype-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Devel] Memory leak using code in tutorial?


From: Hari Nair
Subject: [Devel] Memory leak using code in tutorial?
Date: Fri, 12 Jul 2002 09:43:44 -0700
User-agent: Internet Messaging Program (IMP) 4.0-cvs

Using the code I've appended to the bottom of this
message, which is pretty much a cut and paste of
the code in the freetype tutorial, I find memory
leaks using "valgrind":

With 2.0.9:

 ==2067== 240 bytes in 6 blocks are definitely
lost in loss record 1 of 2
==2067==    at 0x40046474: malloc
(vg_clientfuncs.c:100)
==2067==    by 0x4024383C: ft_alloc
(/home/hari/local/src/freetype-2.0.9/builds/unix/ftsystem.c:102)
==2067==    by 0x40244526: FT_Alloc
(/home/hari/local/src/freetype-2.0.9/src/base/ftobjs.c:65)
==2067==    by 0x402486EB: ft_new_glyph
(/home/hari/local/src/freetype-2.0.9/src/base/ftglyph.c:364)
==2067== 
==2067== 1596 bytes in 18 blocks are still
reachable in loss record 2 of 2
==2067==    at 0x40046474: malloc
(vg_clientfuncs.c:100)
==2067==    by 0x4024383C: ft_alloc
(/home/hari/local/src/freetype-2.0.9/builds/unix/ftsystem.c:102)
==2067==    by 0x40244526: FT_Alloc
(/home/hari/local/src/freetype-2.0.9/src/base/ftobjs.c:65)
==2067==    by 0x40247B64: FT_Outline_New_Internal
(/home/hari/local/src/freetype-2.0.9/src/base/ftoutln.c:266)

With 2.1.2: (The code doesn't display properly
here, either)

==2072== 240 bytes in 6 blocks are definitely lost
in loss record 1 of 2
==2072==    at 0x40046474: malloc
(vg_clientfuncs.c:100)
==2072==    by 0x4024459C: ft_alloc
(/home/hari/local/src/freetype-2.1.2/builds/unix/ftsystem.c:102)
==2072==    by 0x4024488A: FT_Alloc
(/home/hari/local/src/freetype-2.1.2/src/base/ftutil.c:59)
==2072==    by 0x40249707: ft_new_glyph
(/home/hari/local/src/freetype-2.1.2/src/base/ftglyph.c:364)
==2072== 
==2072== 840 bytes in 18 blocks are still
reachable in loss record 2 of 2
==2072==    at 0x40046474: malloc
(vg_clientfuncs.c:100)
==2072==    by 0x4024459C: ft_alloc
(/home/hari/local/src/freetype-2.1.2/builds/unix/ftsystem.c:102)
==2072==    by 0x4024488A: FT_Alloc
(/home/hari/local/src/freetype-2.1.2/src/base/ftutil.c:59)
==2072==    by 0x402462FC: FT_Outline_New_Internal
(/home/hari/local/src/freetype-2.1.2/src/base/ftoutln.c:266)
==2072== 


Here's the code I'm using.  Thanks for any advice!

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H

#define MAX_GLYPHS 256

FT_Library   library;   /* handle to library    
*/
FT_Face      face;      /* handle to face object
*/

void
drawText(char *text)
{
  FT_GlyphSlot  slot = face->glyph;  /* a small
shortcut */
  FT_UInt       glyph_index;
  FT_Bool       use_kerning;
  FT_UInt       previous;
  int           pen_x, pen_y, n;
  int error;
  

  FT_Glyph      glyphs[ MAX_GLYPHS ];   /* glyph
image */
  FT_Vector     pos   [ MAX_GLYPHS ];   /* glyph
position */
  FT_UInt       num_glyphs;

  pen_x = 0;   /* start at (0,0) !! */
  pen_y = 0;

  num_glyphs  = 0;
  use_kerning = FT_HAS_KERNING(face);
  previous    = 0;

  for ( n = 0; n < strlen(text); n++ )
    {
      /* convert character code to glyph index */
      glyph_index = FT_Get_Char_Index( face,
text[n] );

      /* retrieve kerning distance and move pen
position */
      if ( use_kerning && previous && glyph_index
)
        {
          FT_Vector  delta;

          FT_Get_Kerning( face, previous, glyph_index,
                          ft_kerning_default, &delta );

          pen_x += delta.x >> 6;
        }

      /* store current pen position */
      pos[ num_glyphs ].x = pen_x;
      pos[ num_glyphs ].y = pen_y;

      /* load glyph image into the slot. DO NOT
RENDER IT !! */

      error = FT_Load_Glyph( face, glyph_index,
FT_LOAD_DEFAULT );
      if (error) continue;  /* ignore errors, jump
to next glyph */

      /* extract glyph image and store it in our
table */
      error = FT_Get_Glyph( face->glyph, &
glyphs[num_glyphs] );
      if (error) continue;  /* ignore errors, jump
to next glyph */

      /* increment pen position */
      pen_x += slot->advance.x >> 6;

      /* record current glyph index */
      previous = glyph_index;
      
      /* increment number of glyphs */

      num_glyphs++;
    }

  for ( n = 0; n < num_glyphs; n++ )
    {
      FT_Glyph  image;
      FT_Vector pen;
      
      image = glyphs[n];
      
      pen.x = pos[n].x;
      pen.y = pos[n].y;
      
      error = FT_Glyph_To_Bitmap( &image,
ft_render_mode_normal,
                                  &pen, 0 );
      if (!error)
        {
          FT_BitmapGlyph  bit = (FT_BitmapGlyph)image;
          FT_Bitmap bitmap = bit->bitmap;
          int j, k;
          for (j = 0; j < bitmap.rows; j++)
            {
              int istart = j * bitmap.width;
              for (k = 0; k < bitmap.width; k++)
                {
                  if (bitmap.buffer[istart + k])
                    {
                      printf("*");
                    }
                  else
                    {
                      printf(" ");
                    }
                }
              printf("\n");
            }
          printf("\n");
          FT_Done_Glyph( image );
        }
    }
}

int
main(int argc, char **argv)
{
  int error = FT_Init_FreeType( &library );
  assert(error == 0);

  error = FT_New_Face( library,
                      
"/usr/share/fonts/default/TrueType/helr____.ttf",
                       0,
                       &face );
  assert(error == 0);
  
  error = FT_Set_Pixel_Sizes(face, 0, 24);
  assert(error == 0);

  drawText("123321");

  FT_Done_FreeType(library);

  exit(EXIT_SUCCESS);
}



reply via email to

[Prev in Thread] Current Thread [Next in Thread]