Access Violation on Application Startup -
07-06-2006
, 06:51 PM
Hi,
My application has a pure native dll (say some.dll), which didn’t have any
user-provided DllMain. The application also includes some mixed and pure
managed dlls.
In most scenarios it can start up just fine. The platform is WinXP SP2, and
the IDE is VS2005.
Accidentally, I configured the app with Win2K compatibility running mode,
which caused the 0xc0000005 Access violation exception when starting up the
application. “The application failed to initialize properly (0xc0000005).
Click on OK to terminate the application.”
I used Windbg to analyze what happened, and monitored the initializing
process. Finally, found that the SOME!_CRT_INIT function gets called twice,
and it was the 2nd call which caused the exception.
The exception was threw at,
BOOL WINAPI _CRT_INIT(HANDLE hDllHandle, DWORD dwReason, LPVOID lpreserved)
{
.....
if (__native_startup_state != __uninitialized)
{
_amsg_exit( _RT_CRT_INIT_CONFLICT); // second call on it caused
exception
}
else
{ // …
I think the major difference between these two scenarios was usage of
compatibility mode, which leads to load more dlls (like shimeng.dll) and
different loading sequence. That’s the big point I’m very wondering. Why the
CRT init got called twice in W2K compat mode?
Then I added two linker switches while building the some.dll, say,
link -DLL /NOENTRY /INCLUDE:__DllMainCRTStartup@12 $(OriginLinkFlags)
For some reason, this *SEEMED* to have fixed the problem. But I'm still
confused. This change just totally bypassed the SOME!__DllMainCRTStartup and
SOME!_CRT_INIT to avoid duplicated invocation. Why did I have to do that in
WIN2K compatibility mode? BTW, the problem has nothing to do with mixed dll
loading, it’s just a native dll; also it occurred only when configured Win2k
compat mode, but worked well in both W2K and WinXP normal mode.
Can anyone help? Thanks |