Mail Archives: djgpp/1999/11/20/19:35:33
On 20 Nov 99, at 16:47, Laurence Withers wrote:
>
> I have come across something I don't understand when compiling code.
> Whilst writing a small utility, I found that if you catch a C++
> exception in main() and then return, the program will crash if you have
> compiled it with the options '-O -fomit-frame-pointer' (that's -O, -O2
> or -O3).
>
> 'gcc -v':
>
> Reading specs from c:/bin/djgpp/lib/specs
> gcc version 2.95 19990728 (release)
>
> - sample program -------------------------------------------------------
> #include <iostream>
>
> class Exception { };
>
> int main()
> {
> try {
> char c = 0;
> cout << "Throw exception [yY]? ";
> cin.get(c);
> if(c == 'y' || c == 'Y') {
> cout << "Throwing..." << endl;
> throw Exception();
> }
> cout << "No exception..." << endl;
> }
> catch(...) {
> cout << "Exception caught..." << endl;
> return -1;
> }
> cout << "No error." << endl;
> return 0;
> }
> - end ------------------------------------------------------------------
>
> Try compiling with 'gcc -O -fomit-frame-pointer -g -o test.exe test.cpp'
> and then running. If you enter y or Y, the exception will be caught but
> the program will crash. Any other key, and there is no crash.
>
> Now compile without -fomit-frame-pointer, and there is no crash. Compile
> without -O, and there is no crash.
>
This is to say that I also compiled and ran your program using
GCC2.95.2 (the latest) and your problem still is in it.
I compiled your program using "gcc -O3 -fomit-frame-pointer t.cpp -
o t.exe -lstdcx -g"
And it also crashed giving the output below.
Throw exception [yY]? y
Throwing...
Exception caught...
Exiting due to signal SIGSEGV
General Protection Fault at eip=00001704
eax=ffffffff ebx=000a7cdc ecx=00000010 edx=00027884
esi=00000054 edi=0002797c
ebp=000a7970 esp=000a794c program=D:\D\T.EXE
cs: sel=00a7 base=822ae000 limit=000bffff
ds: sel=00af base=822ae000 limit=000bffff
es: sel=00af base=822ae000 limit=000bffff
fs: sel=0087 base=0001ac50 limit=0000ffff
gs: sel=00bf base=00000000 limit=0010ffff
ss: sel=00af base=822ae000 limit=000bffff
App stack: [000a797c..0002797c] Exceptn stack:
[00027858..00025918]
Call frame traceback EIPs:
0x00001704 _main+336, line 19 of t.cpp
Line 19 of your source happens to be the "return -1;" statement.
I am not quite familiar with c++ syntatics but I happened to have a
copy of Borland C 4.5 and with it your program compiled OK and
threw the exception without crashing. So it may be a compiler bug?
Kalum.
- Raw text -