Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

catching exceptions from library

Posted on 2011-09-30
5
Medium Priority
?
428 Views
Last Modified: 2012-06-21
Hello,

My main exe is trying to catch an exception that is thrown from a shared library.  There is an error message in the catch block but program crashes without any message. Both library and main exe  linked with "dinkum with exceptions" option.

I'm using
QNX 6.5.0 with GNU 4.4.2
Could you help me please?
0
Comment
Question by:vileda
  • 2
  • 2
5 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 36894052
Allowing exceptions to percolate module boundaries is a bad idea. The standard C++ exceptions (for example) allocate memory on the heap (they contain a the message returned by the member function 'what()').

Heap allocated in one module should not be destroyed in another since each module has its own heap. When you catch and handle the exception object it will be destroyed and any heap it's allocate will be deleted. If the exception comes from another module this is a problem waiting to happen.
0
 
LVL 29

Expert Comment

by:pepr
ID: 36894250
Is the crash really manifested via an exception?  There are also crashes where no exception is thrown.  They are more low-level situation.
0
 

Author Comment

by:vileda
ID: 36896083
It happens where I put the exception. when the throw line closed it works properly.
 
0
 

Author Comment

by:vileda
ID: 36896131
What is the alternative way the handle errors? "if" checks? Object oriented C++ books encougrage exception usage. Should I make a choice between OOP and shared object library?
0
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 36896518
Unfortunately, C++ is a standard but libraries are not. Libraries are not a C++ thing. If you are using static libs then there is no problem since all code is linked into one module. The issue is that when you link to shared libraries the goal posts change.

A shared library should avoid throwing C++ exceptions across the api boundary. As well as the issue with memory it should also be noted that the applicarion using a library may not even know how to handle C++ exceptions (maybe the consuming language isn't C++ for example).

Except for special cases, you should avoid exception percolation. You would also be best to code the library with C and not C++ linkage and have api functions return error codes.

In you consumer app you can code a C++ wrapper for this api that handles interfacing with the library; checking the result of function calls and throwing a C++ exception.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question