Ok...this is interesting.
When I run my program through the Ubuntu Gnome-Terminal with the same arguments, it works fine. It only produces the problem I posted about when I run it through xterm by pressing the Run button in the Code::Blocks IDE.

So it must be a bug in xterm. I already posted about the issue on the Ubuntu forums.
Hopefully the guys over there will take care of it.