getting mad with DLLs and std:list

Started by
2 comments, last by SiCrane 17 years, 8 months ago
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.
Advertisement
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.
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 ;)
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;}

This topic is closed to new replies.

Advertisement