Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. DLL crashing

DLL crashing

Scheduled Pinned Locked Moved C / C++ / MFC
helpgame-devtestingbeta-testingquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tansey4
    wrote on last edited by
    #1

    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?

    S P L 3 Replies Last reply
    0
    • T tansey4

      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?

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      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

      T 1 Reply Last reply
      0
      • S Stephen Hewitt

        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

        T Offline
        T Offline
        tansey4
        wrote on last edited by
        #3

        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.

        S 1 Reply Last reply
        0
        • T tansey4

          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.

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          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 calls clear someone will notice a problem. Steve

          1 Reply Last reply
          0
          • T tansey4

            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?

            P Offline
            P Offline
            Prakash Nadar
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • P Prakash Nadar

              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

              L Offline
              L Offline
              Laxman9
              wrote on last edited by
              #6

              [Message Deleted]

              P O 2 Replies Last reply
              0
              • L Laxman9

                [Message Deleted]

                P Offline
                P Offline
                Prakash Nadar
                wrote on last edited by
                #7

                thanks, but its not me who wants it :)


                -Prakash

                1 Reply Last reply
                0
                • T tansey4

                  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?

                  L Offline
                  L Offline
                  Laxman9
                  wrote on last edited by
                  #8

                  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:

                  1 Reply Last reply
                  0
                  • L Laxman9

                    [Message Deleted]

                    O Offline
                    O Offline
                    Owner drawn
                    wrote on last edited by
                    #9

                    Laxman9 wrote:

                    FAILURE is the first step towards SUCCESS

                    FAILURE is the last step towards the PINK SLIP

                    Jesus Loves:rose:

                    --Owner Drawn:rose: --Defeat is temporary but surrender is permanent --Jesus is Lord:rose:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups