EDN Admin
Well-known member
In our project we have faced issue where std::string passed by reference gets badly corrupted when passed from a EXE built in debug mode with VS2010 to a VC++ DLL built in release mode with VS2010.
It works fine if exe and the dll are built in release mode or if both are in debug mode but it fails when exe is built in debug mode and dll in release mode, string is corrupted.
I could recreate using the following snippet of code:-
I have created a dll in release mode with the following piece of code :-
In Sampleheader.h file declaration :-
#define DLLDIR __declspec(dllexport) // export DLL information
#include <string>
class DLLDIR HelloDLL
{
public:
HelloDLL(void);
~HelloDLL(void);
void function1(const std::string & str);
};
In Sampledllfile.cpp,
#include "sampdllheader.h"
#include <iostream>
using namespace std;
HelloDLL::HelloDLL(void)
{
}
HelloDLL::~HelloDLL(void)
{
}
void HelloDLL::function1(const std::string & str)
{
cout << "In function1 str " <<str.data() << endl;
}
Created the application in debug mode:-
I have created exe in debug mode linking the dll created with above piece of code. Note that dll is built in release mode. So exe built in debug mode uses the dll built in release mode :-
#include <sampdllheader.h>
#include "conio.h"
#include <string>
using namespace std;
int main(int argc, char * argv[])
{
string str = "Test string";
HelloDLL helloDLL;
helloDLL.function1(str);
getch();
return 0;
}
It works fine if dll and exe are built on release mode.
It works fine if dll and exe are built on debug mode.
string contains garbage only if dll is built in release mode and exe in debug mode.
View the full article
It works fine if exe and the dll are built in release mode or if both are in debug mode but it fails when exe is built in debug mode and dll in release mode, string is corrupted.
I could recreate using the following snippet of code:-
I have created a dll in release mode with the following piece of code :-
In Sampleheader.h file declaration :-
#define DLLDIR __declspec(dllexport) // export DLL information
#include <string>
class DLLDIR HelloDLL
{
public:
HelloDLL(void);
~HelloDLL(void);
void function1(const std::string & str);
};
In Sampledllfile.cpp,
#include "sampdllheader.h"
#include <iostream>
using namespace std;
HelloDLL::HelloDLL(void)
{
}
HelloDLL::~HelloDLL(void)
{
}
void HelloDLL::function1(const std::string & str)
{
cout << "In function1 str " <<str.data() << endl;
}
Created the application in debug mode:-
I have created exe in debug mode linking the dll created with above piece of code. Note that dll is built in release mode. So exe built in debug mode uses the dll built in release mode :-
#include <sampdllheader.h>
#include "conio.h"
#include <string>
using namespace std;
int main(int argc, char * argv[])
{
string str = "Test string";
HelloDLL helloDLL;
helloDLL.function1(str);
getch();
return 0;
}
It works fine if dll and exe are built on release mode.
It works fine if dll and exe are built on debug mode.
string contains garbage only if dll is built in release mode and exe in debug mode.
View the full article