Your application may fail with an "Access Violation" error message when you use ODBC or DAO in the InitInstance or DLLMain functions of a DLL (147629)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Editions 4.0
- Microsoft Visual C++, 32-bit Editions 4.1
- Microsoft Visual C++, 32-bit Enterprise Edition 4.2
- Microsoft Visual C++, 32-bit Professional Edition 4.2
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
- Microsoft Data Access Components 2.5
This article was previously published under Q147629 SYMPTOMS If you use ODBC or DAO in the InitInstance or DLLMain
functions of a DLL, your application may fail with an "Access Violation" or
display some other unanticipated behavior. This can occur when you use the MFC
Database Classes (ODBC-based or DAO-based), the ODBC API, or the dbDao classes
provided with the DAO SDK. CAUSE The reason for this behavior is that ODBC drivers, DAO, or
any of their underlying components may spawn multiple threads. Creating threads
during initialization of a Win32 DLL can leave Windows in an undefined state.
When a Win32 DLL is loaded, Windows calls the DLL's DllMain function
with a reason for calling of DLL_PROCESS_ATTACH.
When a new thread
is created in Win32, each DLL in the process space will have its DllMain
function called by Windows. Windows signals that a new thread has been created
by passing a reason for calling of DLL_THREAD_ATTACH.
For a regular
DLL that uses MFC, MFC supplies a DllMain that calls InitInstance. If you call
a function in InitInstance that directly or indirectly creates one or more
threads, you may experience unexpected behavior. This is because your
application is within the context of DllMain (in InitInstance) with a
DLL_PROCESS_ATTACH call, and creating a thread will result in Windows needing
to call your DllMain again with a DLL_THREAD_ATTACH. The behavior resulting
from this action is undefined.
If you are writing an extension DLL,
you must supply your own implementation of the DllMain function. If you
directly or indirectly create threads within this DllMain function, you will
also experience this problem. RESOLUTION Do not create threads explicitly or implicitly within the
context of DllMain.
Provide a function in your DLL for complex
initialization that you can call outside of the scope of DllMain.
STATUS This behavior is by design. REFERENCES "Advanced Windows" (Chapter 11) by Jeffery Richter, ISBN
1-55615-677-4. For more information, please see the following
related articles in the Microsoft Knowledge Base: 138812
Calls to an OLE object should not be done from DllMain
142243
PRB: Cannot create an MFC thread during DLL startup
Modification Type: | Major | Last Reviewed: | 9/1/2005 |
---|
Keywords: | kbDatabase kbinterop kbprb kbprogramming KB147629 kbAudDeveloper |
---|
|