Run the attached file via
root -l -b test.C 2> >(grep 'foo')
which pipes stderr through grep, then kill the grep process.
root.exe will then call it's SIGPIPE signal handler, which in turn tries to write to stderr, resulting in an infinite loop. You'll have to kill the process.
Probably the best way to fix this â€“ without changing anything about the existing functionality â€“ would be to reset the handler to SIG_DFL before anything is printed.