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::cout
stops working after a while. I'm usingboost::asio::io_service
withdeadline_timer
in order to call amy_print()
function.
Thismy_print()
function prints to screen every1 second
some 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::map
with 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
16
isn't printed. - If I call the executable, and redirect
stdout
to a file (./a.out > aaa.txt
) the line of element16
is 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 line16
wasn't drawn,stdout
got stuck (I mean, the program still worked, but nothing was printed tostdout
never 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.failbit
andbadbit
were1
. - If I run the code with
valgrind
this behavior doesn't happen.valgrind
doesn't say anything wrong. - If I run it with
gdb
it happens, butgdb
doesn'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
printWidth
I give for the alignment infprintf()
- WhenprintWidth
is bigger, it happens sooner (when it's smaller, line16
is randomly getting printed). - Another important thing: it happens more frequently when there is more to print.
- I tried to give
std::cout
a 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
NULL
pointer. - I print with
\n
every 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....
}
}