Access Violation After DLL Exception

I have to load modules as dll dynamically at runtime, since they are not known in advance, they just correspond to the class interface. I noticed that after I catch the exception thrown by the dll (in the main program in the main thread), the correct destructors are called and the modules are killed and the dll is unloaded, and then how} at the end of the catch block is reached by the Visual Studio C ++ debugger with step-by-step When executed, I get another exception that the program throws with

First chance exception on 0x68ad2377 (msvcr90d.dll) in xxxxx.exe: 0xC0000005: location where access violation was read 0x02958f14.

If I allow the exception to be thrown, a violation of this second exception shows the location as

msvcr90d.dll! __ DestructExceptionObject (EHExceptionRecord * pExcept = 0x0017ee4c, unsigned char fThrowNotAllowed = 0) String 1803 + 0xf bytes

but it looks like the frame stack may be corrupted. I cannot understand why this exception is thrown.

A simplified version of my code structure is as follows:

Very simplified program structure:

//shared header:
class Module
{
public:
    virtual void Foo(void) = 0;
};


//dll:
class SomeSpecificModule : public Module
{
public:
    virtual void Foo(void);
};

void SomeSpecificModule::Foo(void)
{
    throw 1;
}

extern "C" __declspec(dllexport) Module* GetModule()
{
    return new SomeSpecificModule;
}


//program:
typedef ptrGetModule* (*GetModule)();

int main(void)
{
    HANDLE hMod = LoadLibrary("SomeSpecificModule.dll");
    ptrGetModule GetModule = (ptrGetModule)GetProcAddress(hMod, "GetModule");
    try
    {
        Module *d = GetModule();
        d->Foo();
    }
    catch (...)
    {
        cout << '!' << endl;
    }
    return 0;
}
+3
source share
7 answers

, C . SomeSpecificModule.dll C, . DLL- C. , SomeSpecificModule.dll , .

, DLL , , , , , . SomeSpecificModule.dll try, SomeSpecificModule:: Foo(), , m svcr90d.dll!__DestructExceptionObject(EHExceptionRecord * ...

, DLL . POD, , C , , STL... .

, DLL. - #define, , .

, , , . , .

+4

, , DLL , DLL. DLL, ?

.

+3

? catch.

+1

, DLL ( ). ?

DLL , DLL , , .., , , DLL .

+1

, , DLL

0

, .

, , DEBUG, msvcr90d.dll. dll, DEBUG? msvcr90.dll msvcr90d.dll - dll.

, typedef . :

typedef Module* (*moduleFnType)();

int main(void)
{
    HANDLE hMod = LoadLibrary("SomeSpecificModule.dll");
    moduleFnType GetModule = (moduleFnType)GetProcAddress(hMod, "GetModule");
    try
    {
        Module *d = GetModule();
        d->Foo();
    }
    catch (...)
    {
        cout << '!' << endl;
    }
    return 0;
}

typedef GetModule.

0

Canopus: int , .

TK ___: dll .

Assaf Shing Yip: dll FreeLibrary() , - tr1:: shared_ptr ( STL), try {}. , , , , DLL, , RAII. , , , - . , , , , , , FreeLibrary() , , } catch {}.

Magnus Skog: In release mode, I also get a crash, not an exception trap, and then continued execution in normal mode. Dynamic memory is processed by 1) the new operator in several cases, 2) tr1 :: shared_ptr and 3) _mm_malloc / _mm_free, where I need alignment.

0
source

Source: https://habr.com/ru/post/1710042/


All Articles