I have implemented a DLL including DllMain() entry function:-
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
case DLL_PROCESS_ATTACH:
/* here im doing file memory mapped through CreateFileMapping() API
and using it through MapViewOfFile() API
storing some data structure in the file mapped area */
...
case DLL_THREAD_ATTACH:
...
case DLL_THREAD_DETACH:
...
case DLL_PROCESS_DETACH:
/* Here unmapping first through UnmapViewOfFile() API
then tries to access the stroed data structure which is illegal
as we have already closed the file mapping
This is the buggy code. The order should be first access the mapped memory
region and then unmap it */
cout <<" some message"<<endl;
...
}
Unfortunately i made a mistake in DLL_PROCESS_DETACH case and accessing illegal memorey (access violation).
I made a sample program which loads the library using LoadLibrary() function, uses the library function and finally call FreeLibrary() and return.
When i executed this program, i didnt get any error message. But if i remove FreeLibrary(), in that case the DLL_PROCESS_DETACH case is executed implicitly and this time it gives error dialog box mentioning that there is access violation.
Why calling FreeLibrary() suppress this error? OR internally it handles this exception? What is suggested way.
Update: I have added details for ATTACH and DETACH. Probably it will help why i am not clear about the behavior observed. With FreeLibrary() call, i didnt get any error message but the cout message was not displayed. It seems crashed too but not being bnotified. But if i remove FreeLibrary(), in that case the DLL_PROCESS_DETACH case is executed implicitly and gives access violation dialog box. Ideally in former case as well, it should display the error. So i am guessing the FreeLibrary() suppresses this access violation error.