Skip to content

Don’t Forget To Flush…

Categories: Cocoa, Software

No, not the toilet, your file streams.

I was recently working on a project that involved creating a child process and reading its stdout to update the user inteface. Everything was working great until I sent it off to some testers who reporting that it ‘wasn’t working’ – the interface wasn’t updating correctly. I first thought it was a problem with the shell command itself, maybe it wasn’t performing the operation correctly and thus wasn’t sending the correct message through stdout but as far as I could tell everything was working perfectly. After about 3 hours of banging my head against the wall trying random things I finally thought to flush the stdout after sending my status message. It fixed it! But why would it would fine on my laptop (intel), my desktop (ppc), but not my old laptop (ppc)? The problem arose from the fact that right after sending the data to stdout the child process would call a function that would block indefinitely. I learned my lesson, always flush the stream if you are depending on reading that output in a timely manner!