Solved

catching exceptions from library

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Socket Programming (Unix) 8 124
is twain_32.dll cmpatible with windows 10 ? 10 155
object oriented programming comparison 5 69
Installshield for Embarcadero EX 10.1 Berlin 4 37
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…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

813 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

11 Experts available now in Live!

Get 1:1 Help Now