DLL crashing
-
I recently wrote a simulator for a card game, and I wanted people to have the ability to extend the default AI by writing their own. I set it up so that people could write a DLL and the simulator would look for it. However, when I run the simulator using the DLL, it crashes at the start of the 2nd hand. The wierd part is that if I copy/paste the code from the DLL to the actual simulator, it runs fine. The problem occurs when I try to access (not dereference) a pointer which is passed to the DLL's function. This is the code I'm using to access the dll:
string dllName = "BotAI.dll"; const char* functionName = "?getBotDecision@@YA?AVAction@@AAVHandHistory@@PAVStatsDB@@_N@Z"; HMODULE module = LoadLibrary(dllName.c_str()); // our function pointer for the function we defined in the dll Action (*theDllFunc)( HandHistory&,StatsDB*,bool) = 0; if (!module) { cout << dllName << " not found or load failed.\n" << endl; Sleep( 10000 ); exit(1); } // retrieve our function from the .dll by name theDllFunc = (Action (*)(HandHistory&,StatsDB*,bool))GetProcAddress(module, functionName); Action result(ACTION_Fold);//fold is the default action if (!theDllFunc) { cout << functionName << " not found inside " << dllName << " or load failed.\n" << endl; Sleep(10000); } else { // load was sucessful // we can now use the function pointer to call the function result = theDllFunc(hand, stats, verbose ); } // important, free the dll if (!FreeLibrary(module)) cout << "error releasing the module loaded from " << dllName << endl; return result;
The strange part as I said is that it runs fine for 1 hand using the DLL for all 10 players, then hand #2 starts and it crashes while accessing a function call to StatsDB. However if I put all the code into the actual simulator and comment out all the above code, it works fine. Note that I'm compiling the DLL with the same compiler as the simulator, and am using the same static library for the DLL as for the simulator. Also, it accesses the DLL fine for the first hand, and only crashes on the 2nd hand. Each player accesses the DLL only once per hand, so I am inclined to think it is something to do with accessing the DLL twice from the same player...or something along those lines. As I've said, it works for the entire simulation if I just copy/paste the code from the dll to the simulator, and I've done a lot of simulator testing, so that's not what's wrong. Any ideas? -
I recently wrote a simulator for a card game, and I wanted people to have the ability to extend the default AI by writing their own. I set it up so that people could write a DLL and the simulator would look for it. However, when I run the simulator using the DLL, it crashes at the start of the 2nd hand. The wierd part is that if I copy/paste the code from the DLL to the actual simulator, it runs fine. The problem occurs when I try to access (not dereference) a pointer which is passed to the DLL's function. This is the code I'm using to access the dll:
string dllName = "BotAI.dll"; const char* functionName = "?getBotDecision@@YA?AVAction@@AAVHandHistory@@PAVStatsDB@@_N@Z"; HMODULE module = LoadLibrary(dllName.c_str()); // our function pointer for the function we defined in the dll Action (*theDllFunc)( HandHistory&,StatsDB*,bool) = 0; if (!module) { cout << dllName << " not found or load failed.\n" << endl; Sleep( 10000 ); exit(1); } // retrieve our function from the .dll by name theDllFunc = (Action (*)(HandHistory&,StatsDB*,bool))GetProcAddress(module, functionName); Action result(ACTION_Fold);//fold is the default action if (!theDllFunc) { cout << functionName << " not found inside " << dllName << " or load failed.\n" << endl; Sleep(10000); } else { // load was sucessful // we can now use the function pointer to call the function result = theDllFunc(hand, stats, verbose ); } // important, free the dll if (!FreeLibrary(module)) cout << "error releasing the module loaded from " << dllName << endl; return result;
The strange part as I said is that it runs fine for 1 hand using the DLL for all 10 players, then hand #2 starts and it crashes while accessing a function call to StatsDB. However if I put all the code into the actual simulator and comment out all the above code, it works fine. Note that I'm compiling the DLL with the same compiler as the simulator, and am using the same static library for the DLL as for the simulator. Also, it accesses the DLL fine for the first hand, and only crashes on the 2nd hand. Each player accesses the DLL only once per hand, so I am inclined to think it is something to do with accessing the DLL twice from the same player...or something along those lines. As I've said, it works for the entire simulation if I just copy/paste the code from the dll to the simulator, and I've done a lot of simulator testing, so that's not what's wrong. Any ideas?I can't see any obvious mistakes. Can you include more details of the crash? ie. The exact line it crashes, the exception type and a stack trace of the crash is a bare minimum. Steve
-
I can't see any obvious mistakes. Can you include more details of the crash? ie. The exact line it crashes, the exception type and a stack trace of the crash is a bare minimum. Steve
Hi Steve, I just spent a long while debugging, and it seems that the app is crashing when I call clear() on an stl map which is a member variable of an object which is a member variable of StatsDB. Not sure at all why that it's happening here. The message I get while debugging is: Unhandled exception at 0x1007619d (BotAI.dll) in Simulator.exe: 0xC0000005: Access violation reading location 0xcdcdce06. The actual line it dies is:
void _Erase(_Nodeptr _Rootnode) { // free entire subtree, recursively for (_Nodeptr _Pnode = _Rootnode; !_Isnil(_Pnode); _Rootnode = _Pnode) { // free subtrees, then node _Erase(_Right(_Pnode)); _Pnode = _Left(_Pnode); this->_Alnod.destroy(_Rootnode); // destroy, free erased node this->_Alnod.deallocate(_Rootnode, 1); } } Line 3 of the above pasted segment.
-
Hi Steve, I just spent a long while debugging, and it seems that the app is crashing when I call clear() on an stl map which is a member variable of an object which is a member variable of StatsDB. Not sure at all why that it's happening here. The message I get while debugging is: Unhandled exception at 0x1007619d (BotAI.dll) in Simulator.exe: 0xC0000005: Access violation reading location 0xcdcdce06. The actual line it dies is:
void _Erase(_Nodeptr _Rootnode) { // free entire subtree, recursively for (_Nodeptr _Pnode = _Rootnode; !_Isnil(_Pnode); _Rootnode = _Pnode) { // free subtrees, then node _Erase(_Right(_Pnode)); _Pnode = _Left(_Pnode); this->_Alnod.destroy(_Rootnode); // destroy, free erased node this->_Alnod.deallocate(_Rootnode, 1); } } Line 3 of the above pasted segment.
I can't see a call to
clear
. I not sure if the problem can be diagnosed from the code you’ve provided. Perhaps if you show the code which callsclear
someone will notice a problem. Steve -
I recently wrote a simulator for a card game, and I wanted people to have the ability to extend the default AI by writing their own. I set it up so that people could write a DLL and the simulator would look for it. However, when I run the simulator using the DLL, it crashes at the start of the 2nd hand. The wierd part is that if I copy/paste the code from the DLL to the actual simulator, it runs fine. The problem occurs when I try to access (not dereference) a pointer which is passed to the DLL's function. This is the code I'm using to access the dll:
string dllName = "BotAI.dll"; const char* functionName = "?getBotDecision@@YA?AVAction@@AAVHandHistory@@PAVStatsDB@@_N@Z"; HMODULE module = LoadLibrary(dllName.c_str()); // our function pointer for the function we defined in the dll Action (*theDllFunc)( HandHistory&,StatsDB*,bool) = 0; if (!module) { cout << dllName << " not found or load failed.\n" << endl; Sleep( 10000 ); exit(1); } // retrieve our function from the .dll by name theDllFunc = (Action (*)(HandHistory&,StatsDB*,bool))GetProcAddress(module, functionName); Action result(ACTION_Fold);//fold is the default action if (!theDllFunc) { cout << functionName << " not found inside " << dllName << " or load failed.\n" << endl; Sleep(10000); } else { // load was sucessful // we can now use the function pointer to call the function result = theDllFunc(hand, stats, verbose ); } // important, free the dll if (!FreeLibrary(module)) cout << "error releasing the module loaded from " << dllName << endl; return result;
The strange part as I said is that it runs fine for 1 hand using the DLL for all 10 players, then hand #2 starts and it crashes while accessing a function call to StatsDB. However if I put all the code into the actual simulator and comment out all the above code, it works fine. Note that I'm compiling the DLL with the same compiler as the simulator, and am using the same static library for the DLL as for the simulator. Also, it accesses the DLL fine for the first hand, and only crashes on the 2nd hand. Each player accesses the DLL only once per hand, so I am inclined to think it is something to do with accessing the DLL twice from the same player...or something along those lines. As I've said, it works for the entire simulation if I just copy/paste the code from the dll to the simulator, and I've done a lot of simulator testing, so that's not what's wrong. Any ideas?is BotAI your dll? if yes, that that is not the way to export and use a function from a DLL. That name you are using i.e ?getBotDecision@@YA?AVAction@@AAVHandHistory@@PAVStatsDB@@_N@Z is a result of namemangling done by the C++ compiler and it may change if you change the compiler or change the function signature. Best way is to use dllimport dllexport directivies so that the compiler does not do any name mangling to the function. Read more on how to import/export functions from the dll.
-Prakash
-
is BotAI your dll? if yes, that that is not the way to export and use a function from a DLL. That name you are using i.e ?getBotDecision@@YA?AVAction@@AAVHandHistory@@PAVStatsDB@@_N@Z is a result of namemangling done by the C++ compiler and it may change if you change the compiler or change the function signature. Best way is to use dllimport dllexport directivies so that the compiler does not do any name mangling to the function. Read more on how to import/export functions from the dll.
-Prakash
-
thanks, but its not me who wants it :)
-Prakash
-
I recently wrote a simulator for a card game, and I wanted people to have the ability to extend the default AI by writing their own. I set it up so that people could write a DLL and the simulator would look for it. However, when I run the simulator using the DLL, it crashes at the start of the 2nd hand. The wierd part is that if I copy/paste the code from the DLL to the actual simulator, it runs fine. The problem occurs when I try to access (not dereference) a pointer which is passed to the DLL's function. This is the code I'm using to access the dll:
string dllName = "BotAI.dll"; const char* functionName = "?getBotDecision@@YA?AVAction@@AAVHandHistory@@PAVStatsDB@@_N@Z"; HMODULE module = LoadLibrary(dllName.c_str()); // our function pointer for the function we defined in the dll Action (*theDllFunc)( HandHistory&,StatsDB*,bool) = 0; if (!module) { cout << dllName << " not found or load failed.\n" << endl; Sleep( 10000 ); exit(1); } // retrieve our function from the .dll by name theDllFunc = (Action (*)(HandHistory&,StatsDB*,bool))GetProcAddress(module, functionName); Action result(ACTION_Fold);//fold is the default action if (!theDllFunc) { cout << functionName << " not found inside " << dllName << " or load failed.\n" << endl; Sleep(10000); } else { // load was sucessful // we can now use the function pointer to call the function result = theDllFunc(hand, stats, verbose ); } // important, free the dll if (!FreeLibrary(module)) cout << "error releasing the module loaded from " << dllName << endl; return result;
The strange part as I said is that it runs fine for 1 hand using the DLL for all 10 players, then hand #2 starts and it crashes while accessing a function call to StatsDB. However if I put all the code into the actual simulator and comment out all the above code, it works fine. Note that I'm compiling the DLL with the same compiler as the simulator, and am using the same static library for the DLL as for the simulator. Also, it accesses the DLL fine for the first hand, and only crashes on the 2nd hand. Each player accesses the DLL only once per hand, so I am inclined to think it is something to do with accessing the DLL twice from the same player...or something along those lines. As I've said, it works for the entire simulation if I just copy/paste the code from the dll to the simulator, and I've done a lot of simulator testing, so that's not what's wrong. Any ideas?here is the sample..code for DLL //header file dllexportsclass.h// #ifdef DLLEXPORTSCLASS_EXPORTS #define DLLEXPORTSCLASS_API __declspec(dllexport) #else #define DLLEXPORTSCLASS_API __declspec(dllimport) #endif / This class is exported from the DllExportsClass.dll class DLLEXPORTSCLASS_API CDllExportsClass { public: void show(); CDllExportsClass(void); // TODO: add your methods here. }; extern DLLEXPORTSCLASS_API int nDllExportsClass; DLLEXPORTSCLASS_API int fnDllExportsClass(void); //c++ file// include the above header file / This is an example of an exported variable DLLEXPORTSCLASS_API int nDllExportsClass=0; // This is an example of an exported function. DLLEXPORTSCLASS_API int fnDllExportsClass(void) { ::MessageBox(NULL,"function","",0); return 42; } // This is the constructor of a class that has been exported. CDllExportsClass::CDllExportsClass() { return; } void CDllExportsClass::show() { ::MessageBox(NULL,"Hello Dll","CDllExportsClass",0); } and include the header file into the project where u want to use the DLL. Thanks and Regards Laxman FAILURE is the first step towards SUCCESS :cool: -- modified at 0:30 Monday 30th January, 2006 Thanks and Regards Laxman FAILURE is the first step towards SUCCESS :cool:
-
Laxman9 wrote:
FAILURE is the first step towards SUCCESS
FAILURE
is the last step towards thePINK SLIP
Jesus Loves:rose:
--Owner Drawn:rose: --Defeat is temporary but surrender is permanent --Jesus is Lord:rose: