Solved

catching exceptions from library

Posted on 2011-09-30
5
413 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 28

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now