title: Segmentation Fault when loading font with FreeType
body: <p>I'm trying to use the FreeType library together with libpng to output a PNG image of a glyph. My code compiles just fine but I then run into a segmentation fault. Here's some debugging output from gdb (I'm not sure what to make of it):</p>
  
  <pre><code>(gdb) run
  Starting program: /home/david/Desktop/a.out 
  
  Program received signal SIGSEGV, Segmentation fault.
  0x00007ffff76a58a5 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
  (gdb) where
  #0  0x00007ffff76a58a5 in ?? () from /lib/x86_64-linux-gnu/libc.so.6
  #1  0x00007ffff7bcb298 in png_write_row (png_ptr=0x611320, row=&lt;optimized out&gt;)
      at /usr/include/x86_64-linux-gnu/bits/string3.h:52
  #2  0x00007ffff7bcb56c in png_write_image (png_ptr=0x611320, image=0x60d218)
      at pngwrite.c:608
  #3  0x0000000000400c35 in main ()
  </code></pre>
  
  <p>And here's the code:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;png.h&gt;
  
  #include &lt;ft2build.h&gt;
  #include FT_FREETYPE_H
  
  main() {
  
      // Declare FreeType variables
      FT_Library library;
      FT_Face face;
      FT_GlyphSlot slot;
      FT_UInt glyph_index = 30;
      char* font_file = "/usr/share/fonts/truetype/freefont/FreeMono.ttf";
  
      // Declare PNG variables
      png_uint_32 width = 100;
      png_uint_32 height = 100;
      int bit_depth = 16;
      int color_type = PNG_COLOR_TYPE_GRAY;
      char* file_name = "/home/david/Desktop/out.png";
      png_structp png_ptr;
      png_infop info_ptr;
  
      // Render font
      int error;
      error = FT_Init_FreeType(&amp;library);
      error = FT_New_Face(library, font_file, 0, &amp;face);
      error = FT_Set_Pixel_Sizes(face, 100, 100);
      error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
      error = FT_Render_Glyph(face-&gt;glyph, FT_RENDER_MODE_NORMAL);
  
      if (error) {
          printf("ERROR CODE: %d", error);
      }
  
      // Create a PNG file
      FILE *fp = fopen(file_name, "wb");
  
      // Create the PNG in memory
      png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
      info_ptr = png_create_info_struct(png_ptr);
      png_init_io(png_ptr, fp);
  
      // Write the header
      png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
      png_write_info(png_ptr, info_ptr);
  
      // Write image data
      slot = face-&gt;glyph;
      png_write_image(png_ptr, &amp;slot-&gt;bitmap.buffer);
  
      // End write
      png_write_end(png_ptr, NULL);
  
      fclose(fp);
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I'm trying a slightly new approach (using png_write_row instead of png_write_image).</p>
  
  <pre><code>... SAME AS BEFORE ABOVE ....
  
  // Write image data
  slot = face-&gt;glyph;
  
  int x, y, offset;
  for (y = 0; y &lt; slot-&gt;bitmap.rows; y++) {
      for (x = 0; x &lt; slot-&gt;bitmap.width; x++) {
          offset = y*(slot-&gt;bitmap.width) + x;
          png_write_row(png_ptr, &amp;slot-&gt;bitmap.buffer[offset]);
      }
  }
  
  // End write
  png_write_end(png_ptr, NULL);
  
  fclose(fp);
  </code></pre>
  
  <p>}</p>
  
  <p>Now I get some more useful libpng errors, but I'm still not sure what to make of them:</p>
  
  <pre><code>libpng warning: zstream not in use (internal error)
  libpng error: stream error
  Aborted (core dumped)
  </code></pre>