I had a similar problem in conjunction with inline assembly once.
-O3 would make my application segfault while -O2 would not, and I did not have a clue why. That is surely a compiler-related issue, though, definitely has nothing to do with code::blocks.
This kind of bug is very annoying because you really get no useful hints. Without a starting point, no one will be able to help you either.
Try this:
go to
http://gcc.gnu.org/onlinedocs/gcc-3.4.4/gcc/Optimize-Options.html#Optimize-OptionsThere, you find what exactly is turned on by -O, -O2, and -O3.
Now, change to -O or -O2 (whichever still works for you) and add the other options by hand. So you get the very same thing, it still crashes.
Then, take away those extra switches one by one. At some point, it will work. Now you do not have solved the problem, but at least you have a starting point (or your can choose to just leave off one switch).