Problem writing PNG-files using SDL_RWops

Hello dear List,

this question might be off-topic here (and libpng-related), yet I cannot
determine where it belogs. Therefore please redirect me to the
appropriate list if that is so.

I’m having troubles saving PNG-files from within my SDL application. I’m
using a piece of code from Mike Oliphant’s sfontmaker package (named
png_save.c). In this code the accessing of the libpng-functions is
wrapped so it is available just like an SDL call using the SDL_RWops
struct which I believe is from SDL_image.h.

The code usually works fine. Yet it stopped working after (I guess) my
png-files exceeded a certain size (around 500kB). The problem is as
follows: when the PNG-file is written, it is appearently not synced to
disk. When I try to read the file after just having written to it, the
last 2400 bytes (always 2400 bytes) are missing. A
system("/bin/ls -l file.png") also shows the file is 2400 bytes too
little. And stat says the same. Therefore, reading the image failes
weirdly (i.e. the last few sprites in that image are quite messed up,
some warnings, one error). A strange thing is that even sleeping
(SDL_Delay or sleep, same effect) say 20 seconds after having the image
written does not flush the whole thing.

When the application quits and I’m back on the command line, the file
magically now has the correct size, and reading works fine when the
application is subsequently started (obviosly regenerating the sprites
then is not necessary anymore, as they’ve already been created).

This looks very much like a flushing-problem to me. Yet, playing around
with the functions which are being set by png_set_write_fn did not
change a thing.

My current flush routine looks like that (that I changed from the
original package as in the original the author did not implement flushing):
static void png_io_flush(png_structp png_ptr) {
FILE* io_ptr;
io_ptr = (FILE *)CVT_PTR((png_ptr->io_ptr));
printf(“Flushing 0x%8lx 0x%8lx\n”,(unsigned long)png_ptr,(unsigned
if (io_ptr!=NULL) fflush(io_ptr);

Yet, this function is never called by libpng/SDL. Worse: if I call it
manually by inserting the reference as the last line of code of the
png_io_write function, the program dumps core. Is the png_ptr->io_ptr
not a “FILE*”?

I hope somebody can help me with this problem. As said, I’m not sure if
this is a SDL issue or libpng related. Any advice very welcome.