FIX: Problems with Using the MFC DAO Classes in a .DLL or .OCXLast reviewed: October 13, 1997Article ID: Q143084 |
The information in this article applies to:
SYMPTOMSTwo errors can occur when using the MFC DAO database classes in a DLL. Depending upon the context of how you use the MFC DAO classes in the DLL, you can receive an assertion on line 36 of the Daocore.cpp or you may receive an access violation.
CAUSEThe root of the problem exists in how MFC is terminating DAO. MFC calls AfxDaoTerm from the exit instance of CWinApp. This is fine for applications but isn't a good thing to do for DLLs because ExitInstance() is called from the DLL_PROCCESS_DETACH case. Use of OLE and specifically DAO should be avoided in the ExitInstance() of a DLL. Another problem with the MFC DAO code is that it stores the pointer to the database engine on a global scope so that if one regular DLL that links to MFC dynamically closes and calls AfxDaoTerm, it closes the database engine for any other DLLs which use DAO and link dynamically to MFC. The line 36 assertion is caused by the fact that an exception gets thrown in AfxDaoTerm when trying to call Release() on the database engine. The exception prohibits the setting of m_pDAOEngine pointer back to NULL. Again, this problem occurs because MFC incorrectly calls DAO in ExitInstance() of a DLL. Here is the code in AfxDaoTerm():
// Clean up engine object if necessary if (pDaoState->m_pDAODBEngine != NULL) { pDaoState->m_pDAODBEngine->Release(); pDaoState->m_pDAODBEngine = NULL; }You can see that if an exception occurs in Release(), the m_pDAODBEngine pointer is not set to NULL. Thus, the assert on line 36 of Daocore.cpp will occur:
ASSERT(m_pDAODBEngine == NULL);An access violation can occur if you perform the following sequence:
This occurs because the DLL1 has already shut down the DAO components. There may be other scenarios that can cause other access violations to occur. For example, do not create global MFC DAO objects within a DLL. The resolution section below doesn't address this problem because it is something that should not be done.
RESOLUTIONTo work around the problems with DAO in a DLL, do one of the following:
STATUSMicrosoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ 4.1.
REFERENCESFor more information about problems using DAO in DLLs, please refer to the following articles in the Microsoft Knowledge Base:
ARTICLE ID: Q149889 TITLE : PRB: Assertion or Problems Using DAO in a DLL ARTICLE ID: Q152315 TITLE : PRB: When to Call AfxDaoTerm() in an Automation Server Keywords : MfcDAO vcbuglist400 vcfixlist410 kbprg kbbuglist kbfixlist Technology : kbMfc Version : 4.0 Platform : NT WINDOWS Issue type : kbbug Solution Type : kbfix |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |