Why does my app crash in fwrite()?

I have random crashes when calling fwrite()…

I have random crashes when calling fwrite()…

MacOS X buffers data written to files in RAM to increase performance. The trouble is that there is an unfortunate interaction with virtual memory that causes the OS to become really confused sometimes when you write to a large file, causing your app to crash.

The solution for this is to periodically flush the cache for your file when you know you'll be writing lots of data. This is done by calling fflush() on the file. But note that you shouldn't do that too often, or your app will suffer very bad performance.