Re: Exception handling on AIX with static-libgcc - Segmentation fault On Mar 17, 11:29*am, devsh...@gmail.com wrote:
> Hi,
> I'm having a weird problem with exception handling on AIX5.3 with gcc
> 3.4.6.
> After couple of tests I find out *that in order to work with
> exceptions on AIX, I must use shared lib-gcc instead of the static
> one.
>
> Pleas see my code below and my conclusions and let me kwnow, how can I
> solve this problem.
> (The following is the simplest code I could reproduce the problem
> with)
>
> -------------
> main.cpp
> -------------
> #include <string>
> #include <iostream>
> using namespace std ;
>
> class XXX_DAD
> {
> public:
> * *XXX_DAD() {m_sReason="dad's reason";}
> * *virtual ~XXX_DAD(){};
> * *string GetReason() {return m_sReason;}
> protected:
> * *string m_sReason;
>
> };
>
> class XXX : public XXX_DAD
> {
> public:
> * *XXX(string sInput): XXX_DAD(){m_sExp=sInput;}
> * *virtual ~XXX() {};
> * *string GetExp() {return m_sExp;}
> private:
> * *string m_sExp;
>
> };
>
> void gTest()
> {
> * XXX* s = new XXX("ben ben...");
> * throw s;
>
> }
>
> int main()
> {
> * *try
> * *{
> * * * * *try
> * * * * *{
> * * * * * * gTest();
> * * * * *}
> * * * * *catch(XXX* ex)
> * * * * *{
> * * * * * * printf("XXX exception was caught and re-throw: %s\n", ex->GetExp().c_str());
>
> * * * * * * throw;
> * * * * *}
> * *}
> * *catch (XXX_DAD* ex_dad)
> * *{
> * * * printf("XXX_DAD was caught: %s\n", ex_dad->GetReason().c_str());
> * *}
> * *return 0;
>
> }
>
> ---------------------------------
> Compilation command:
> ---------------------------------
> gcc -o tester_static main.cpp -L/usr/lib/threads -pthread -I. -static-
> libgcc -L/usr/lib -ldl -lpthread `gcc -print-file-name=libstdc++.a` -o
> * `gcc -print-file-name=libstdc++.a` *is /usr/local/lib/gcc/powerpc-
> ibm-aix5.3.0.0/3.4.6/../../../libstdc++.a
>
> This compilation will generate the "tester_static" executable.
> If I run it I get the following output:
> * * *XXX exception was caught and re-throw: ben ben ...
> * * *Segmentation fault (core dumped)
> as you can see, the first exception wat caught as it should, but when
> it was re-thrown in order to be caugth by the base exception, I got
> "Segmentation fault (core dumped)"
>
> =====
> Notes:
> =====
> 1. If instead of calling gTest(), I will create new XXX object and
> throw the exception directly (without using a function) as you can see
> below, everything works well and I got the following desired output
> (both derived and based exception were caught)
> .....
> try
> * *{
> * * * * *try
> * * * * *{
> * * * * * * XXX* s = new XXX("ben ben...");
> * * * * * * throw s;
> * * * * *}
> * * * * *catch(XXX* ex)
> * * * * *{
> * * * * * * printf("XXX exception was caught and re-throw: %s\n", ex->GetExp().c_str());
>
> * * * * * * throw;
> * * * * *}
> * *}
> ....
>
> desired output:
> XXX exception was caught and re-throw: ben ben ...
> XXX_DAD was caught: dad's reason
>
> 2. If I used the exact same code, but link it with shared lib-gcc,
> everything works well and I also get the desired output (both derived
> and based exception were caught)
>
> here is the compilation command***:
> gcc -o tester_shared main.cpp -I. -shared-libgcc -L/usr/lib -ldl -
> lpthread -lstdc++
>
> *** The problem here is that I don't want to dynamically link libgcc
> => I must use the static lib-gcc!!!
> here is the ldd output of the "tester_shared":
> /usr/local/lib/gcc/powerpc-ibm-aix5.3.0.0/3.4.6/../../../
> libgcc_s.a(shr.o)
> /usr/lib/libpthread.a(shr_xpg5.o)
> /usr/lib/libpthread.a(shr_comm.o)
> ./tester_shared
> /usr/lib/libcrypt.a(shr.o)
> /usr/lib/libc.a(shr.o)
>
> and this is (libgcc_s.a(shr.o)...) not good for me, since I don't want
> to distribute also libstdc++.a with my products executables
>
> Any suggestions???
B.T.W
if I throw and catch the exception by val instead of using pointers,
the program will always crash on run-time with the following error:
terminate called after throwing an instance of 'XXX'
IOT/Abort trap (core dumped) |