PDA

View Full Version : exception in shared libraries



big4mil
4th December 2006, 19:51
Hi all,

does somebody has experience handling exceptions within shared libraries on linux?

I can dynamically load my shared library, but if I call a member function in my library which throws exceptions and try to catch it in my main program, I'll get following error:
Program received signal SIGABRT, Aborted.
Terminate called after throwing a instance of 'DBException'.
(In debug mode).

Loading the library in my main app:


typedef DBFactory *create_t();
create_t *create_object = (create_t*)QLibrary::resolve( "./libmylib.so", "create" );

if( create_object )
{
qDebug() << "Library loaded!!!" << '\n';
dbFac = create_object();
try
{
dbFac->checkConnection(); // in this function the exception will be thrown
// but unfortunately the program crashs
}
catch( DBException &dbEx)
{
...
}
}


Any help would be appreciated.
Best regards
big4mil

Chicken Blood Machine
5th December 2006, 04:23
Exceptions cannot always safely be thrown across shared library boundaries.
Have you tried linking with you library rather than dynamically loading it?

big4mil
5th December 2006, 19:51
Thanks for your response.

Yes that would be a way to solve my problem, but I prefer to load it dynamically.
I've seen that there is a way to handle exception across shared libraries. The flag RTLD_GLOBAL must be set while loading the library:

void* handle = dlopen( "./libdbconn.so", RTLD_NOW | RTLD_GLOBAL );

The library should then also be build with the following option:

-Xlinker -export-dynamic

Let me try that...
cu
big4mil