Trouble writing PNGs with 16 bits per channel using libpng

I've been working on a more sophisticated height map implementation (hopefully on the way to proper bump mapping) and I've been using PNGs with the red channel as the height data and the green, blue and alpha channels as the x,y and z components of the normals for each vertex.

I wrote a program to read in an image, calculate the normals and write out such a PNG. I got it all working using 8bit precision and it looks ok, but not as good as calculating the normals in the rendering program at higher precision. So I converted my program to use a 16bit colour depth and it works up til writing out the PNG at which point gdb says:

mi_cmd_stack_list_frames: Not enough frames in stack.

This is in png_malloc when in png_create_write_struct2 it seems.

I'm using a test image of 16x16 (the 8bit program works with 512x512 ) so I find it hard to believe there really isn't enough space on the stack, butI'd freely admit I don't really understand what's going on.

if (setjmp(png_jmpbuf(write_ptr)))
{ // on an error libpng needs to know where to jump back to
#ifdef IVORDEBUG_PNG
printf("PNGLoader:writePNGFile: Error during init_io");
#endif
fclose(fp);
return false;
}

png_init_io(write_ptr, fp);

/* turn on or off filtering, and/or choose
specific filters. You can use either a single
PNG_FILTER_VALUE_NAME or the logical OR of one
or more PNG_FILTER_NAME masks. */
png_set_filter(write_ptr, 0, PNG_FILTER_NONE);