C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How do I catch exceptions thrown from C++ functions called w

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Paul Harman
Guest





PostPosted: Fri Jul 16, 2004 1:36 pm    Post subject: How do I catch exceptions thrown from C++ functions called w Reply with quote



I'm on Solaris 8, using g++ to compile. Versions availabe if required... I
have googled for this, but not come up with any useful answers...

I'm writing a C++ (ish) application that uses dynamic libraries loaded using
dlopen and functions called by dlsym. Because of the name mangling this
involves, all of the functions are prototyped inside an extern C block.

These library functions then call object methods that are defined back in
the main application. Unfortunately, the code inside the library is unable
to catch any exceptions thrown. I presume this has something to do with
namespaces or some other kind of name mangling, since other methods in the
main application that call these methods *are* able to catch the exceptions.

Is there an obvious way around this... other than rewriting all of my C++
code so that it doesn't throw exceptions for errors?

Paul


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Richter
Guest





PostPosted: Mon Jul 19, 2004 5:29 pm    Post subject: Re: How do I catch exceptions thrown from C++ functions call Reply with quote



Hi,

Quote:
I'm writing a C++ (ish) application that uses dynamic libraries loaded
using dlopen and functions called by dlsym. Because of the name
mangling this involves, all of the functions are prototyped inside an
extern C block.


Quote:
These library functions then call object methods that are defined back
in the main application. Unfortunately, the code inside the library is
unable to catch any exceptions thrown. I presume this has something to
do with namespaces or some other kind of name mangling, since other
methods in the main application that call these methods *are* able to
catch the exceptions.


Quote:
Is there an obvious way around this... other than rewriting all of my
C++ code so that it doesn't throw exceptions for errors?

The C++ standard itself doesn't say a word about dynamic libraries, or what
has to happen if you throw an exception in a function called by a library,
so in what follows I can only present some observations:

Do you have access to the library source? Is this C or C++? g++ requires a
special stack-layout to be able to perform automatic stack-unwinding, and
this might not be available for the in-between library code. If this library
is your code, you might want to try to compile it as C++. This might help
the compiler to forward exceptions properly.

Another (arkward) approach might be to build a tiny wrapper around the
library calls and around your callee: Before calling the library, use
"setjmp" to collect the current position, catch errors here and generate the
apropriate exceptions for your front-end. In the callee, add another wrapper
around your functions, catch exceptions there and forward them to the caller
by means of "longjmp" (where they are thrown again).
Depending on what the library does, this may or may not work, as all
resources allocated on the way down to the callee are not properly released
(no stack unwinding!). Thus, your milage may vary.

So long,
Thomas




Quote:
Paul


Quote:
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Paul Harman
Guest





PostPosted: Tue Jul 20, 2004 6:11 pm    Post subject: Re: How do I catch exceptions thrown from C++ functions call Reply with quote



Thomas Richter <thor (AT) cleopatra (DOT) math.tu-berlin.de> wrote

Quote:
Do you have access to the library source? Is this C or C++?

Yes, I also write the dynamic libraries involved. It's C++, compiled
using the following:

g++ -Wall -O -g -fPIC -c <whatever.cpp> -o <whatever.o>
ld -G <whatever.o> -o <whatever.so>

Quote:
g++ requires a
special stack-layout to be able to perform automatic stack-unwinding, and
this might not be available for the in-between library code. If this library
is your code, you might want to try to compile it as C++. This might help
the compiler to forward exceptions properly.

Hmmm - see above. Of course all of the functions in the library are
defined as "extern C", so that dlsym can find them - I expect that
this is mangling the ability to unwind the stack.

Quote:
Another (arkward) approach might be to build a tiny wrapper around the
library calls and around your callee: Before calling the library, use
"setjmp" to collect the current position, catch errors here and generate the
apropriate exceptions for your front-end. In the callee, add another wrapper
around your functions, catch exceptions there and forward them to the caller
by means of "longjmp" (where they are thrown again).

The software architect in me is screaming out in horror... yes, that
might well work, but before going to those extremes I think I'd rather
write versions of the utility functions that don't throw exceptions
and instead return NULL pointers, or whatever is appropriate.

Thanks,

Paul

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ben Hutchings
Guest





PostPosted: Tue Jul 20, 2004 9:22 pm    Post subject: Re: How do I catch exceptions thrown from C++ functions call Reply with quote

Paul Harman wrote:
Quote:
I'm on Solaris 8, using g++ to compile. Versions availabe if required... I
have googled for this, but not come up with any useful answers...

I'm writing a C++ (ish) application that uses dynamic libraries loaded using
dlopen and functions called by dlsym. Because of the name mangling this
involves, all of the functions are prototyped inside an extern C block.

These library functions then call object methods that are defined back in
the main application. Unfortunately, the code inside the library is unable
to catch any exceptions thrown. I presume this has something to do with
namespaces or some other kind of name mangling, since other methods in the
main application that call these methods *are* able to catch the exceptions.

I think extern "C" is irrelevant to this. It shouldn't affect
exception handling at all.

I suspect that the cause of the problem is that the DSOs (dynamic
libraries) and the main executable are each using their own separate
copies of the type information for the exception classes. If the
exception-handling code compares type information by address then it
would see the exception thrown in the main executable as having a
different type from the type expected by the handler in the DSO.

Quote:
Is there an obvious way around this... other than rewriting all of my C++
code so that it doesn't throw exceptions for errors?

There should be some way to sort this out, but it appears to be a
platform-specific problem that you should ask about on a Unix or
Solaris programming group. I don't know enough about DSOs and C++
to offer a solution.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Eugene Gershnik
Guest





PostPosted: Tue Jul 20, 2004 9:32 pm    Post subject: Re: How do I catch exceptions thrown from C++ functions call Reply with quote

Paul Harman wrote:
Quote:
I'm on Solaris 8, using g++ to compile. Versions availabe if
required... I have googled for this, but not come up with any useful
answers...

I'm writing a C++ (ish) application that uses dynamic libraries
loaded using dlopen and functions called by dlsym. Because of the
name mangling this involves, all of the functions are prototyped
inside an extern C block.

These library functions then call object methods that are defined
back in the main application. Unfortunately, the code inside the
library is unable to catch any exceptions thrown.

I have seen the same with Sun's compiler and the reason there was that the
shared library and the main application used different versions of C++
runtime. Make sure you compile all parts of your application (main and
shared libs) using the same compiler settings and standard libraries.

--
Eugene



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Abrahams
Guest





PostPosted: Wed Jul 21, 2004 11:57 am    Post subject: Re: How do I catch exceptions thrown from C++ functions call Reply with quote

Ben Hutchings <do-not-spam-benh (AT) bwsint (DOT) com> writes:

Quote:
I think extern "C" is irrelevant to this. It shouldn't affect
exception handling at all.

I suspect that the cause of the problem is that the DSOs (dynamic
libraries) and the main executable are each using their own separate
copies of the type information for the exception classes. If the
exception-handling code compares type information by address then it
would see the exception thrown in the main executable as having a
different type from the type expected by the handler in the DSO.

I was going to say that, but when I had this sort of problem on
GNU/Linux their people investigated Solaris and found that it *didn't*
occur there. Sun CC compares typeinfo by comparing hashes and then
comparing strings if the hashes collide. So this is a real mystery.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Paul Harman
Guest





PostPosted: Wed Jul 21, 2004 7:05 pm    Post subject: Re: How do I catch exceptions thrown from C++ functions call Reply with quote

"Eugene Gershnik" <gershnik (AT) hotmail (DOT) com> wrote

Quote:
I have seen the same with Sun's compiler and the reason there was that the
shared library and the main application used different versions of C++
runtime. Make sure you compile all parts of your application (main and
shared libs) using the same compiler settings and standard libraries.

Indeed. I've fixed this now.

I was using 'ld' to link the libraries, but 'g++' to compile & link
the main application. Now that I'm using 'g++' to link the libraries,
the exceptions all seem to work properly.

I'm also passing RTLD_GLOBAL to dlopen()... not sure if that is
helping or not, but it seems good advice.

Paul

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Joerg Richter
Guest





PostPosted: Wed Jul 21, 2004 8:13 pm    Post subject: Re: How do I catch exceptions thrown from C++ functions call Reply with quote

Hi Paul,

Quote:
Yes, I also write the dynamic libraries involved. It's C++, compiled
using the following:

g++ -Wall -O -g -fPIC -c <whatever.cpp> -o <whatever.o
ld -G

Try using g++ to build your shared libs (and executables):

g++ -shared -o

Joerg

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
David Abrahams
Guest





PostPosted: Wed Jul 21, 2004 8:15 pm    Post subject: Re: How do I catch exceptions thrown from C++ functions call Reply with quote

[email]chatterbox (AT) doctorwhowebguide (DOT) net[/email] (Paul Harman) writes:

Quote:
"Eugene Gershnik" <gershnik (AT) hotmail (DOT) com> wrote

I have seen the same with Sun's compiler and the reason there was that the
shared library and the main application used different versions of C++
runtime. Make sure you compile all parts of your application (main and
shared libs) using the same compiler settings and standard libraries.

Indeed. I've fixed this now.

I was using 'ld' to link the libraries, but 'g++' to compile & link
the main application. Now that I'm using 'g++' to link the libraries,
the exceptions all seem to work properly.

I'm also passing RTLD_GLOBAL to dlopen()... not sure if that is
helping or not, but it seems good advice.

Yes, if you're using g++, it's helping. Not sure if it's good advice
-- it's really a double-edged sword. With g++ it fixes the
typeinfo/exception problems, but dlopen is usually used for plugin
type architectures, and RTLD_GLOBAL makes you vulnerable to symbol
collisions from plugin authors.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.