Solved

catching exceptions from library

Posted on 2011-09-30
5
418 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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 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