I've now tracked down and fixed the bug. Ironically it's Mirar that caused it (granted a long time ago) by "#define unsigned int size_t" which isn't valid on a 64-bit machine. I believe it could have caused overwriting of as much memory as the resulting JPEG image occupied outside of the initial buffer size.