std::string going out of scope
i'm sure the whole stl being used in a dll thing has been discussed a lot already, but i always seem to have 1 specific problem, as demonstrated by this code:
Code:
#include "stdafx.h"
#include "MyLibrary.h"
int main(int argc, char* argv[])
{
Node node;
node.x = 900;
node.y = 955;
node.description = "node 1";
return 0;
}
MyLibrary is a dll project, and the MyLibrary header includes this definition od Node:
Code:
class ITINARARYFINDA_API Node {
public:
float x;
float y;
std::string description;
};
the problem is, that there is a debug assertion failed error on expression _CrtIsValidHeapPointer(pUserData) at runtime, when the main function returns. i'm fairly sure it occurs when the std::string goes out of scope, since i've had similar problems before.
am i doing something wrong here?
Re: std::string going out of scope
There are problems exporting classes using templates as discussed here http://www.codeproject.com/cpp/MemberTemplateDLL.asp .
std:string is a template STL class so I'm you're probably experiencing the same sorts of problems.
Re: std::string going out of scope
Quote:
Originally Posted by Bob Sheep
these seem to be linker errors, but i dont have a problem compiling it, just at runtime..
useful link though, thanks very much.
Re: std::string going out of scope
can u post your test project, I'll look at it with some diagnostice software I have here.
2 Attachment(s)
Re: std::string going out of scope
Quote:
Originally Posted by Bob Sheep
can u post your test project, I'll look at it with some diagnostice software I have here.
thanks, the dll project is call itinararyfinda and the app is called iftestapp.
Re: std::string going out of scope
I get this error compiling the dll with Visual Studion 2003
warning C4251: 'Itinarary::coords' : class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class 'Itinarary'
I do not get a crash however but the cause is explained here
http://support.microsoft.com/kb/q168958/
http://support.microsoft.com/kb/q172396/
Re: std::string going out of scope
Quote:
Originally Posted by peterworth
i'm sure the whole stl being used in a dll thing has been discussed a lot already,
I guess you missed the many discussions that both the app and DLL must use the DLL version of the runtime library. The reason why is that classes that allocate memory internally in one module must use the same heap manager when deallocating in another module. Only the DLL runtime library ensures this.
Also another issue with what you're doing is that you will get in a lot of trouble with your application if the app uses a different version of the string class as the DLL. In other words, your DLL will only work with an app created with the exact same version and service pack build of Visual C++ as the DLL was created with. For example, if you created the DLL with the stock version of Visual C++, and I create an app that uses the Visual C++ STLPort version of the STL classes, your DLL cannot be used by my application.
Regards,
Paul McKenzie
Re: std::string going out of scope
is my dll not using the dll version of the runtime library then? how do i fix that?
Re: std::string going out of scope
This is another KB article which explains how to export STL classes.
It also says you require < and == operator functions to be defined as well.
http://support.microsoft.com/default...b;EN-US;168958
Because I had similar problems I ended up writing my own map, array classes to manage void pointers and then wrapped these basic classes in a class specific to each object type.