DLL Global Object Constructor String Error
Hello,
This one has really got my head spinning. I cannot figure it out.
I have a c++ console application that loads a dll.
In that dll there is a global variable that is a user defined object.
Upon the dll being loaded the dll calls the object constructor to instantiate the global variable (as expected).
In the constructor I try to use a const string which I have declared in an included file, but I get an error.
The error I recieve is a SIGSEGV, which to me usually means I am referencing an array past its bounds.
I have recreated this in the following simplified files. There are two projects 1) quickhook which calls the dll quickdll.dll, and 2) quickdll which is the dll loaded in quickhook.
quickhook's main.cpp
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
const string QUICKDLL_DLL = "E:\\C++\\codeblocks\\Practice\\quickdll\\bin\\Debug\\quickdll.dll";
if (!(hinstDLL = LoadLibrary((LPCTSTR)QUICKDLL_DLL.c_str()))) {
DWORD dwErr1 = ::GetLastError();
cout << "Loading Library Failed:" << dwErr1 << endl;
}
//OTHER CODE
return 0;
}
quickdll's main.cpp
#include "main.h"
#include <string>
#include "myclass.h"
using namespace std;
MyClass myclass;
quickdll's myclass.h
#ifndef MYCLASS_H_INCLUDED
#define MYCLASS_H_INCLUDED
#include <string>
using namespace std;
class MyClass {
public:
string str;
MyClass();
};
#endif // MYCLASS_H_INCLUDED
quickdll's myclass.cpp
#ifndef MYCLASS_CPP_INCLUDED
#define MYCLASS_CPP_INCLUDED
#include "myclass.h"
#include <string>
#include "const.h"
using namespace std;
MyClass::MyClass() {
this->str = USER_SN;
}
#endif // MYCLASS_CPP_INCLUDED
quickdll's const.h
#ifndef CONST_H_INCLUDED
#define CONST_H_INCLUDED
#include <string>
using namespace std;
//User Constant
const string USER_SN = "MEMEME";
#endif // CONST_H_INCLUDED
The following is the files used
Here is the backtrace from,
#0 712CAD96 std::string::assign() (E:\C++\codeblocks\Practice\quickdll\bin\Debug\quickdll.dll:??)
#1 712CCD45 std::string::operator=() (E:\C++\codeblocks\Practice\quickdll\bin\Debug\quickdll.dll:??)
#2 712C1291 MyClass::MyClass(this=0x712d0030) (E:/C++/codeblocks/Practice/quickdll/myclass.cpp:12)
#3 712C1421 __static_initialization_and_destruction_0(__initialize_p=1, __priority=65535) (E:/C++/codeblocks/Practice/quickdll/main.cpp:9)
#4 712C1458 global constructors keyed to myclass() (E:/C++/codeblocks/Practice/quickdll/main.cpp:10)
#5 712C605F __do_global_ctors() (E:\C++\codeblocks\Practice\quickdll\bin\Debug\quickdll.dll:??)
#6 712C10E1 DllMainCRTStartup@12() (E:\C++\codeblocks\Practice\quickdll\bin\Debug\quickdll.dll:??)
#7 7C90118A ntdll!LdrSetAppCompatDllRedirectionCallback() (C:\WINDOWS\system32\ntdll.dll:??)
#8 712C0000 ??() (??:??)
#9 00000001 ??() (??:??)
#10 00000000 ??() (??:??)
When I debug this I get the sigsegv with the above back trace.
What is going on here? This one has me losing my hair... What "lesson" from programming 101 did I miss?
Thanks,