I have this strange problem with stdout/stderr.
I want to apologize for not being able to put here the original code, it's too long / too many libraries dependent etc...
So please let me know if you ever encountered anything like it, or what may cause this issue without getting the original code, only the idea and examples of the simple things I tried to do:
- I'm using
g++ (GCC) 4.4.6 20120305 (Red Hat 4.4.6-4)onRHEL 6.3 - I couldn't isolate the problem for putting it here, I'll give code examples of what I did.
fprintf() / printf() / std::coutstops working after a while. I'm usingboost::asio::io_servicewithdeadline_timerin order to call amy_print()function.
Thismy_print()function prints to screen every1 secondsome information.
In order to print, I use alignments, like the following:
fprintf(stdout, "%*s\n", -printWidth, someEnumToStr[i]);
fprintf(stdout, "%s\n", aString);
fprintf(stdout, "%u\n", num);
While aString is a std::string. Sometimes I construct aString from std::ostringstream.
Sometimes I construct it with snprintf().
- I have an
std::mapwith information, exactly 16 elements inside the map. I iterate over it, and for each element I try to print data with the example offprintf()above. - For an unknown reason, the line of element
16isn't printed. - If I call the executable, and redirect
stdoutto a file (./a.out > aaa.txt) the line of element16is getting printed. - If I open a new
FILE*andfprintf()to this file, again, everything is getting printed (all lines, including line of element16) - Before using
fprintf()I tried to usestd::cout(and alignments withstd::cout.width(printWidth) << std::left...), The same behavior happened, but when line16wasn't drawn,stdoutgot stuck (I mean, the program still worked, but nothing was printed tostdoutnever again. I had to callstd::cout.clear()for it to work again). Since a point in the code, which I couldn't lay my hands on,std::cout.failbitandbadbitwere1. - If I run the code with
valgrindthis behavior doesn't happen.valgrinddoesn't say anything wrong. - If I run it with
gdbit happens, butgdbdoesn't say anything wrong. - If I run it in an IDE (
clion) in debug mode, it doesn't happen. - If I run it in IDE, without debug, it happens.
- I figure it depends on the
printWidthI give for the alignment infprintf()- WhenprintWidthis bigger, it happens sooner (when it's smaller, line16is randomly getting printed). - Another important thing: it happens more frequently when there is more to print.
- I tried to give
std::couta bigger buffer (not his default) and it didn't work. - I tried to buffer all of the output into a buffer (instead of printing each line), then to only
fprintf()once. Same behavior happens. - I didn't find anywhere in the code I try to print a
NULLpointer. - I print with
\nevery couple offprintf()s, and dofflush()in the end ofmy_print()
Please let me know if you know anything.
Illustration:
deadline_timer..... every 1 sec... my_print()
boost::asio::io_service.run
my_print() {
for(std::map<>::iterator... begin, end, ++it....) {
fprintf()s....
}
}