Sign in to follow this  
WickedImp

getting mad with DLLs and std:list

Recommended Posts

WickedImp    122
Got a problem with using a DLL which is using std::list while inserting an element with push_front(), etc... DEF_EXPORT is defined as __declspec( dllexport ) while compiling DLL and set to __declspec( dllimport ) while linked into EXE Following code in DLL-Header:
	class DEF_EXPORT cElement {
		string			m_Name;
	public:
		cElement ( string sName );
		~cElement ();
		string			getName();
	};

	class DEF_EXPORT cMainObject {
	protected:
		list<cElement*>		m_List;
	public:
		cMainObject ();
		~cMainObject ();
		int			addElement( string sName );
		void			delElement( string sName );
	};
And here some code from the DLL-CPP-File:
	EBYTE cMainObject::addElement( string sName ) {
		cElement *hTmp;

		hTmp = new cElement ( sName );
		if( !hTmp ) return 0;

		this->m_List.push_front( hTmp );
		
		return 1;
	}
Compiling the DLL and exporting the LIB is no problem at all... Now I want to actually use the cMainObject to add an element:
cMainObject *hObj = new cMainObject();
hObj->addElement( "test" );
Program will fail in cMainObject::addElement() on calling push_front()-method. I guess it has something to do with memory used by DLL and memory used by EXE. But I do not know what to do to make this work. Any suggestions? Btw... I use VC 7.1 on WinXP with Built-In STL stuff. For DLL the configuration type is DLL (of course) and runtime library code generation is set to 'Multi-threading Debug DLL'. The EXE uses same code generation.

Share this post


Link to post
Share on other sites
SiCrane    11839
In order to use an STL class from a DLL you need to export the STL class from the DLL properly. Here is an article describing the syntax necessary. However, IIRC, the std::list class doesn't export cleanly from a DLL.

I strongly suggest that you rework your DLL interface so that you export abstract base classes from the DLL and create allocation/deallocation functions from the DLL that return and free class instances based on pointers.

Share this post


Link to post
Share on other sites
WickedImp    122
Ok, thanks for reply. Tried to follow the instructions on the linked page. Got all the warnings they said. But still get the crash. Btw... what do you mean by "export abstract base classes". I'm still a newbie on C++ and OO-Programming... ANSI C worked just fine for me in most cases ;)

Share this post


Link to post
Share on other sites
SiCrane    11839
Keep in mind that if that string type you're using is std::string, you'll need to export that as well, since it's also a template type.

As for the abstract base class thing, in the header you'd have something like:

class IMainObject {
public:
virtual ~IMainObject() = 0;
virtual int addElement(string sName) = 0;
virtual void delElement(string sName) = 0;
};

DLL_EXPORT IMainObject * CreateMainObject(void);
DLL_EXPORT void DeleteMainObject(IMainObject * main_object);

Then in the source file you'd do something like:

class CMainObject : public IMainObject {
public:
int addElement(string sName) {
// blah blah blah
}
void delElement(string sName) {
// blah blah blah
}
};

IMainObject * CreateMainObject(void) {
return new CMainObject();
}
void DeleteMainObject(IMainObject * main_object) {
delete main_object;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this