DLL crash due to memory corruption

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mant
    New Member
    • Mar 2008
    • 6

    DLL crash due to memory corruption

    Hi,
    I have a DLL created in Borland C++, which I am trying to call at runtime using DLL LoadLibrary() from one of my Visual studio C++ project which is a DLL again. And both these are used in a .Net application.

    Following is the way I am trying to export and call.

    Borland C++ DLL exposing the function,

    __declspec(dlle xport) const char * __stdcall Function(char *paramStr1, char *paramStr2, int paramStr3)
    {
    // function body
    // allocates memory - global variables and static variables
    }
    ---------------
    Visualt Studio C++ DLL calling the function,

    HINSTANCE mHInstance;

    typedef char* (__stdcall *BorlandDll) (const char*, const char*, int);
    BorlandDll _BorlandDll;

    _BorlandDll= (BorlandDll)::G etProcAddress(m HInstance, "Function" );

    std::string returnvalue= _BorlandDll(par amStr1, paramStr2, paramStr3);


    The borland Dll decodes an encoded message sent to it from VS C++ Dll by allocating memory(globaly and statically) and it makes call to another project which also allocates memory by using malloc.
    When the Dll called for the first time and if the encoded message is small things are working fine, but if I try to call the Dll function over the same encoded message multiple times(max 5 times) the Dll crashes... Also if the encoded message is huge(upto 150 bytes) the Dll again crashes...

    I get this following error
    "Unhandled exception at 0x32554c54 in TestPerDLL.exe: 0xC0000005: Access violation reading location 0x00000012."

    Remeber the borland C++ dll also calls some code which allocated huge memory.

    Can somebody help me out from this. Or point me to a place where in I can refer to this kind of issues dealt before.

    I also wanted to know where these memory allocation happens. In such case what is the right way of calling the DLL.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Memory for global variables is allocated when the DLL loads. However, to intialize these variables you need a DllMain(). This function is called when the dll loads and unloads (DETACH) so heap resources can be cleaned up.

    I assume you can call the function using GetProcAddress( ). If you can't, post again for more info.

    Comment

    • Mant
      New Member
      • Mar 2008
      • 6

      #3
      The problem, was with the cross compilation. I was using both the Borland and Windows C++ compiler to build my project. I removed the dependecy for the Borland compiler by creating a Visual Studio project and it didnt crash.
      But didnt understand what was the cause for the crash.
      Anybody knows why???

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Using two compilers should no be an issue if one compiler did the DLL and the other did the program. That is, once the bits are in place whatever compiler was used is irrelevant.

        However, using two compilers to build the program or to build the dll will cause problems since the decorator (the name namgler) is not specified by the C++ standard. Each compiler is free to use its own implementation.

        Comment

        Working...