wxString in .cpp in dll causes heap error Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

wxString in .cpp in dll causes heap error

Post by mael15 » Thu Dec 03, 2009 10:45 am

i dont get it:

i have a simple object in a statically linked dll like this:

Code: Select all

class DECLDIR test{
	public:
		test(wxString _str);
		~test(){};

	protected:
		void setStrg(wxString _string) {
			str = _string;
		}
		wxString str;
};
while DECLDIR is

Code: Select all

#ifndef DLL_EXPORT
#define DECLDIR __declspec(dllimport)
#else
#define DECLDIR __declspec(dllexport)
#endif
when i put the constructor in a .cpp file, i get a winheap error:

Code: Select all

test::test(wxString _str){
	setStrg(_str);       // works in cpp
	setStrg(_("test"));  // heap error!
};
when i put the same code in the .h header file, setStrg(_("test")) works.

i instantiate the object with

Code: Select all

test* t = new test(_("whatever"));
why is this?!?!?
thanx!!

ninja9578
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 236
Joined: Thu Jan 29, 2009 3:33 pm

Post by ninja9578 » Thu Dec 03, 2009 2:40 pm

That's not proper code. _("test") is not a wxString, it's a char set, which can be converted into a wxString, but the resulting wxString is const

What I recommend doing if you're new to C++ is to first try to compile your program all together (no dlls,) that way the compiler will tell you if you have parameters that don't match up or something, then pull out what you want into a dll. That's what I do and I've been using dynamic libraries for years.

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Post by mael15 » Thu Dec 03, 2009 3:09 pm

thanx for your thought!

but i still do not understand, why it works in the .h and not in the .cpp file. it does not change a thing if i use

Code: Select all

wxString string("test", wxConvUTF8);
setStrg(string);
instead.

what you recommend is exactly what i did. first have everything in the application, then in the dll. it worked in the app, not in the dll.

so, the problem remains. any other ideas?!

illnatured
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 234
Joined: Mon May 08, 2006 12:31 pm
Location: Krakow, Poland

Post by illnatured » Thu Dec 03, 2009 6:35 pm

If it was std::string, I'd recommend not to use it, because of the infamous DLL allocation pitfall (you must not allocate objects in EXE and free them in DLL or vice versa). I'm not sure whether this applies to wxString too, but probably it does. Try changing wxString to const wxChar* in all of your argument lists in the class member functions. You can still use wxString internally in the DLL, but probably not as an argument for a member function that can be called from the EXE.
VC++ 2005 / Windows XP / wxWidgets 2.8.9

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Post by mael15 » Mon Dec 07, 2009 10:47 am

thanx, it worked when i changed every wxString into cont wxChar*. i do not understand why, but that´s ok.

Post Reply