This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

c++ exceptions in shared library results in crash


first of all, i want to apologize for cross-posing, but i think this
question is of interest for both lists.

now my problem:
i created a main program and a shared library, both written in c++, where
the source is parially taken from the "c++ dlopen mini howto".

find the sources in the attachments!

throwing an exception in the shared library results in a core dump of the
binary executable - but only for the cross-compiled version!

the native linux and cygwin compiled versions work.
so, could someone give me a hint, what's wrong with my cross-compiler?

my cross-compiler (verson 3.2.3) runs under linux and creates cygwin
executables.

thnx!

-- 
rainer hochreiter             | web:    www.hochreiter.at
lilienfelder str. 43          | e-mail: rainer@hochreiter.at
a-3150 wilhelmsburg / austria | mobile: +43 664 2812175

Attachment: configure.ac
Description: Binary data

Attachment: Makefile.am
Description: Binary data

#include "exception.hpp"
#include <iostream>

using std::cout;
using std::cerr;
using std::endl;

MyException::MyException(const std::string& reason) throw()
	: reason_(reason) 
{ 
	cout << "create MyException with reason: " << reason_ << endl;
} 

MyException::~MyException() throw() { }

const char* MyException::what() const throw() { 
	return reason_.c_str();
}
#ifndef __CLASS_EXCEPTION_HPP__
#define __CLASS_EXCEPTION_HPP__

#include <string>
#include <stdexcept>

class MyException : public std::exception {
public:
	MyException(const std::string& reason) throw();

	virtual ~MyException() throw();

	virtual const char* what() const throw();

private:
	const std::string reason_;
};

#endif // __CLASS_EXCEPTION_HPP__
#include "polygon.hpp"
#include "exception.hpp"
#include <dlfcn.h>
#include <iostream>

using std::cout;
using std::cerr;
using std::endl;

int main () {

	cout << "C++ dlopen demo" << endl << endl;

	try {
		// load the library
#if defined(__CYGWIN__)
		const std::string libname("cygcppdll-0.dll");
#else
		const std::string libname("libcppdll.so");
#endif
		cout << "Opening library " << libname << "..." << endl;
		void* handle = dlopen(libname.c_str(), RTLD_LAZY);
		if(handle == 0) {
			cout << "dlopen() failed: " << dlerror() << endl;
			return 1;
		}

		// load the symbols
		cout << "Load symbols from library..." << endl;
		create_t* create_triangle = (create_t*) dlsym(handle, "create");
		destroy_t* destroy_triangle = (destroy_t*) dlsym(handle, "destroy");
		if(create_triangle == 0 || destroy_triangle == 0) {
			cout << "dlsym() failed: " << dlerror() << endl;
			return 1;
		}

		// create instance of class
		cout << "Create class instance..." << endl;
		polygon* poly = create_triangle();

		// use class
		cout << "Use class..." << endl;
		poly->set_side_len(7);
		cout << "poly area is " << poly->area() << endl;

		cout << "Create exception in class..." << endl;
		poly->crash("throw exception");


		// destroy class
		cout << "Destroy class..." << endl;
		destroy_triangle(poly);

		// close library
		cout << "Close library..." << endl;
		dlclose(handle);
		return 0;
	} catch (const MyException& e) {
		cout << "Caught MpException: " << e.what() << endl;
		return 1;
	} catch (const std::exception& e) {
		cout << "Caught std::exception: " << e.what() << endl;
		return 1;
	} catch (...) {
		cout << "Caught unknown exception" << endl;
		return 1;
	}
}				
#ifndef __CLASS_POLYGON_HPP__
#define __CLASS_POLYGON_HPP__

#include <string>

class polygon {
public:
	polygon() : side_length_(0) { }

	void set_side_len(double side_length) {
		side_length_ = side_length;
	}

	virtual double area() const = 0;

	virtual double crash(const std::string& reason) const = 0;

protected:
	double side_length_;

private:
};

typedef polygon* create_t();
typedef void destroy_t(polygon*);

#endif // __CLASS_POLYGON_HPP__
#include "exception.hpp"
#include "polygon.hpp"
#include <cmath>
#include <iostream>

using std::cout;
using std::cerr;
using std::endl;

class triangle : public polygon {
public:
	virtual double area() const {
		cout << "area()..." << endl;
		return side_length_ * side_length_ * sqrt(3) / 2;
		cout << "area()...done." << endl;
	}

	virtual double crash(const std::string& reason) const {
		cout << "crash()...." << endl;
		throw MyException(reason);
		// NEVER REACH THIS
		cout << "crash()...done." << endl;
	}
};


// class factories
extern "C" polygon* create() {
	return new triangle;
}

extern "C" void destroy(polygon* p) {
	delete p;
}
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]