Hello,
I have a dll that I wrote and a driver program to call that dll. Both the programs were written in VC++ 2008 express edition. The program takes in 2 strings as a command line argument and passes it to the dll which prints the 2 strings in the standard output. The problem that I have is when I pass the 2 strings and execute the program I get the following
Debug Assertion Failure
Program:....
File:..\src\dbgheap.c
Line:1317
Expression: _CrtIsValidHeapPointer(pUserData)
This error also occurs only for certain command line arguments such as
"C:\Test Folder\Test Folder" "C:\Test Folder" or
"Winter Spring Summer Fall" "Seasons"
If the command line arguments were as follows
"Winter" "Spring"
the program executes without any error.
Also I would like to point out that while compiling both the dll and the exe, I used the following flag for the Code Generation under the project properties.
Runtime Library: Multi-threaded Debug (/MTd)
If I change the Runtime Library to Multi-threaded Debug DLL (/MDd), the program works fine for all the above command line arguments.
Please do not tell me that I should be using the Multi-threaded Debug DLL (/MDd). It is for some reason I need to use Multi-threaded Debug (/MTd).
I am also providing you the code for the dll and the driver.
CreateDLL.h
#include <string>
using namespace std;
#ifndef HEADER_H
#define HEADER_H
#ifdef EXPORT
#define DLL_EXPORT __declspec( dllexport )
#else
#define DLL_EXPORT __declspec( dllimport )
#endif
extern "C" DLL_EXPORT int StringMani(string a, string b);
#endif
CreateDLL.cpp
#include <iostream>
#include <string>
#include "CreateDLL.h"
using namespace std;
extern "C" int StringMani(string a, string b)
{
char *one,
*two;
int len1,
len2;
len1 = a.length() + 1;
len2 = b.length() + 1;
one = new char[len1];
two = new char[len2];
strcpy(one, a.c_str());
strcpy(two, b.c_str());
cout << "a is: " << one << endl;
cout << "b is: " << two << endl;
delete[] one;
delete[] two;
return 1;
}
Driver.cpp
#include <iostream>
#include <string>
#include "CreateDLL.h"
#include <windows.h>
using namespace std;
int main(int argc, char* argv[])
{
int ret_val;
string a,
b;
a = argv[1];
b = argv[2];
HINSTANCE hDLL;
int(*fn)(string, string);
hDLL = LoadLibrary("CreateDLL.dll");
if (hDLL)
{
fn = (int (__cdecl *)(string, string)) GetProcAddress(hDLL, "StringMani");
if (fn)
{
ret_val = fn(a, b);
}
}
}
Thanks in advance