GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Dec-2007, 05:31
PiJ PiJ is offline
New Member
 
Join Date: Dec 2007
Location: Thuringia, Germany
Posts: 2
PiJ is on a distinguished road

Shared Libraries with qmake and Exception Handling


Hello to everyone.

I have a big problem with exceptions in a shared library I created myself.

Here's the scenario: I wrote a few classes to create UDP servers and clients which are able to send and receive messages. In the functions of this classes I integrated an exception handling, for example:
CPP / C++ / C Code:
bool ConnLessServer::receive(char* data, long &size, const int buf)
{
	int clientAddr_size = sizeof(clientAddr);
	
	size = recvfrom(socketfd, data, buf, 0,
					(struct sockaddr*) &clientAddr,
					(socklen_t *) &clientAddr_size);
	if (-1 != size)
	{
		return true;
	}
	else if (!isSocketValid(socketfd))
	{
		throw ConnException
		("ConnLessServer::receive: No socket initialized.\n");
	}
	else
	{
		throw ConnException
		("ConnLessServer::receive: No data could be received.\n");
	}
	return false;
}

In case receiving of data failed, it is checked whether a socket has been initialized before or not. If no socket has been initialized, an exception is thrown.

This works fine, since a binary has been created using this sourcefiles. A problem occurs, when I want it to make a shared library using qmake.

Creating the shared library works fine and I am able to use it in another project, like in the following example:
CPP / C++ / C Code:
void CHAPacketReceiver::run()
{
	long size;	     // amount of received data
	char data[MAX_PACKET_SIZE];	// data to be applied for
	CHAConHostPacket tmpConHostPacket;
	bool result;
	
	while(1)
	{	
		setTerminationEnabled(true);		//termination of thread enabled
		result = mBrdNetAccess.receive(data, size, MAX_PACKET_SIZE);
		setTerminationEnabled(false);		//termination of thread disabled

		if (result)
		{// a packet has been received
			tmpConHostPacket.setRecvPacket(data, size);
			mRecvPacketQueue->push(tmpConHostPacket);
		}
	}
}

The object mBrdNetAccess was created before and a socket was opened, so in this case everything works fine while receiving data.
Now I tried to throw an exception like in the upper sourcecode by not initializing a socket before. The exception is caught in a main:
CPP / C++ / C Code:
int main(void)
{
	try
	{
		ConHostAdapter m;		
	}
	catch (ConnException& e)
	{
		e.getException();
	}
	catch (CHAException& e)
	{
		e.getErrorMessage();
	}
	catch (...)
	{
		fprintf(stdout, "Unknown exception\n");
	}

	return 0;
}

ConnException should catch the exceptions that are thrown by the shared library. CHAException catches the exceptions thrown by the source code which uses the shared library.

The problem is, that when the exception like in the first example is thrown, then I always get a message like this:

Code:
terminate called after throwing an instance of 'ConnException' Aborted (core dumped)

I just want the exception to get caught by the catch handler in the main.
I cannot find a reason for this, the only thing I found out by searching the internet is, that it seems so be a compiler problem.

For creating the library I used qmake, here is the project file:
Code:
###################################################################### # Automatically generated by qmake (2.01a) So Dez 16 17:07:37 2007 ###################################################################### TEMPLATE = lib VERSION = 1.10.0 DESTDIR = library TARGET = nta DEPENDPATH += . ../src/nta INCLUDEPATH += . ../src/nta CONFIG += dll CONFIG += build_all # Input HEADERS += ../src/nta/ConnCommon.h \ ../src/nta/ConnException.h \ ../src/nta/ConnLessClient.h \ ../src/nta/ConnLessServer.h \ ../src/nta/ConnOrientClient.h \ ../src/nta/ConnOrientServer.h SOURCES += ../src/nta/ConnCommon.cpp \ ../src/nta/ConnException.cpp \ ../src/nta/ConnLessClient.cpp \ ../src/nta/ConnLessServer.cpp \ ../src/nta/ConnOrientClient.cpp \ ../src/nta/ConnOrientServer.cpp

Maybe someone of u can help me with this.

I am using Ubuntu, kernel version 2.6.20-16-generic and gcc version 4.1.2.
  #2  
Old 18-Dec-2007, 06:50
davis
 
Posts: n/a

Re: Shared Libraries with qmake and Exception Handling


By default, Qt builds for Debian and its variants (including Ubuntu) turns exceptions off. (-fno-exceptions) If you want to enable them, you'll need to rebuild Qt with exceptions enabled.

It is entirely possible for you to have two Qt installations on your machine, one with and one without exceptions, but it does require more "management" from you to ensure that you are using the right set of libraries both at link-time and at run-time.

:davis:
  #3  
Old 18-Dec-2007, 10:14
PiJ PiJ is offline
New Member
 
Join Date: Dec 2007
Location: Thuringia, Germany
Posts: 2
PiJ is on a distinguished road

Re: Shared Libraries with qmake and Exception Handling


Hello davis and thanks for the reply.

I followed your advice and reinstalled qt (4.3.3) by using the source, not the package manager. So I was able to enable the exception option (besides, I think the same option u can set in the project file by using CONFIG += exception). I got the same results as before.

At the moment I don't think it is a problem with the library but a problem with the exception handling and that what I wrote is not thread safe.
I tried to catch the exception the function in the library throws directly in the source file:

CPP / C++ / C Code:
void CHAPacketReceiver::run()
{
	try
	{
	long size;								// amount of received data
	char data[MAX_PACKET_SIZE];				// data to be applied for
	CHAConHostPacket tmpConHostPacket;
	bool result;
	
	while(1)
	{	
		setTerminationEnabled(true);		//termination of thread enabled
		result = mBrdNetAccess.receive(data, size, MAX_PACKET_SIZE);
		setTerminationEnabled(false);		//termination of thread disabled

		if (result)
		{// a packet has been received
			tmpConHostPacket.setRecvPacket(data, size);
			mRecvPacketQueue->push(tmpConHostPacket);
		}

	}
	}
	catch (ConnException& e)
	{
		e.getException();
	}

This seems to work fine. So I think, the problem is, catching the exception in the main does not work, because it is another thread.

Does anyone have an idea about that?
  #4  
Old 18-Dec-2007, 10:29
davis
 
Posts: n/a

Re: Shared Libraries with qmake and Exception Handling


Try to reproduce the problem outside of Qt. Try to write something very, very simple using a benign "shared library" that you write with only one function that throws an exception. Build it and test it to see if it exhibits the same behavior. Without all of your code, I can't really help you debug it...and I probably don't want all of it either!

If you have a dead simple Linux console app that does exhibit the problem you're mentioning, I can take a look at it (and so can others here, too) and let you know what you can do to fix it.


:davis:
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:15.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.