Mem Leaks with ATL DLL and CDatabase 
Author Message
 Mem Leaks with ATL DLL and CDatabase


I'm using VC++ 6.0 SP4 on Win 98, and are having problems with a very small
test DLL. The DLL is a small COM component built with the ATL wizard, and
uses MFC for the database communication (ODBC).

I've used Boundschecker to identify some memory leaks, resource leaks and
thread errors that I suspect could lead to a problem I'm having with a
shutdown error message from a larger DLL. So I've built a new, very small
DLL from scratch using the wizards, that recreates the memory leaks and
thread errors.

(Note: I've also tested this on Win 2000. There, only the thread- and
resource leak message appears, NOT the memory leaks.)

The DLL consists of one class with three methods:

CDatabase m_db;

STDMETHODIMP CPtiTest2::AddNumbers(short n1, short n2, short *nSum, short
         *nErrNum = 0;
         *nSum = n1 + n2;
     return S_OK;


     return S_OK;


         m_db.OpenEx( _T("DSN=PTI DB;") );
     return S_OK;


These methods are called from a small VB test program. Calling AddNumbers()
gives no error messages, but calling OpenDB() and CloseDB() results in the
following reported from Boundschecker:

1. Error: Questionable use of thread Thread created using CreateThread
rather than _beginthreadex
Location of Error
AfxUnlockTempMaps winhand.cpp 75
DllMain dllmodul.cpp 170
_DllMainCRTStartup crtdll.c 273

2. Memory leak
128 bytes allocated by malloc in onexit.c (181), HANDLE: 0x036901E0
Location of Error
__onexitinit onexit.c 181

3. Memory leak
350 bytes allocated by malloc in stdenvp.c (122), HANDLE: 0x03690270
Location of Error
_setenvp stdenvp.c 122

4. Memory leak
112 bytes allocated by malloc in stdenvp.c (109), HANDLE: 0x03690ED0
Location of Error
_setenvp stdenvp.c 109

5. Memory leak
24 bytes allocated by malloc in mlock.c (225), HANDLE: 0x03690F50
Location of Error
_lock mlock.c 225

6. Resource leak: allocated by LoadLibraryA in delayhlp.cpp (119), HANDLE:
Location of Error
__delayLoadHelper delayhlp.cpp 119

Since this problem occurs with a very "clean" DLL, my theory is that there's
something I need to call from e.g. CTestPtiComApp::InitInstance() /
ExitInstance() to ensure the memory / threads are handled OK. The docs
states that  AfxDbInitModule() should be called from InitInstance to give
MFC database support, but I could see no difference when I did that.

My questions are:

a) Are these leaks and errors most likely to be my own fault (a sign that
I'm doing something wrong or omitting something), or are these harmless
leaks results of minor MFC flaws?

b) If they are my fault, suggestions on how to fix them?

c) Could it be that these leaks are the source of the page fault I get on a
larger DLL using similar methods?

In advance, thanks for your help.

PS. Should anyone be in a particularly interested and helpful mood today,
wanting to see the problem for themselves, I have a small (45 KB) zip-file
available with a complete example as a Visual Studio workspace file which I
of course gladly send upon request... :-)


PTI Scandpower AS

Sat, 03 May 2003 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Mem leaks with small ATL DLL using CDatabase

2. Mem leaks with small ATL DLL using CDatabase

3. Mem leaks using CDatabase in ATL DLL

4. dll mem leak

5. mem leaks when using std::map in a dll

6. CDatabase::OpenEx loads DLLs, leaks memory?

7. Mem leak in this code?

8. help mem leak with borland C++ opendir closedir

9. Smart pointer and mem leaks

10. Mem leaks & STL

11. mem leak problem

12. Mem leaks & STL


Powered by phpBB® Forum Software