locked
Memory leaks in MFC while using CDatabase::OpenEx() RRS feed

  • Question

  • Hi,

    This question has been asked previously but the explanation was not in detail and i could never reach to the bottom of it.

    I hope i can elaborate more on the problem statement and i can get a resolution/explanation from the experts here.

    Consider the following two sample codes;

    1.

    {
    	int count = 200;
    	for (int i=0; i <count; ++i)
    	{
    		try
    		{
    			CDatabase *db = new CDatabase;
    			
    			BOOL bRes = db->OpenEx(_T("DSN=MyData;UID=anon;PWD=pass"));
    			db->Close();
    
    			delete db;
    			db=NULL;
    		}
    		catch (CDBException* e)
    		{
    			e->Delete();
    		}
    	}
    }

    2.

    {
    	CDatabase *db = new CDatabase;
    	
    	int count = 200;
    	for (int i=0; i <count; ++i)
    	{
    		try
    		{
    			BOOL bRes = db->OpenEx(_T("DSN=MyData;UID=anon;PWD=pass"));
    			db->Close();
    		}
    		catch (CDBException* e)
    		{
    			e->Delete();
    			delete db;
    			db=NULL;
    			return 0;
    		}
    	}
    	
    	delete db;
    	db=NULL;
    }

    The first sample code leaks a lot of memory and it can be easily observed from the task manager, the memory usage keeps on growing.

    The second sample code does not leak any memory if i observe the memory usage from the task manager.

    To find out the cause of the memory leak i ran the code through rational purify, both the codes are leaking memory according to rational purify. The first code leaks significantly more memory than the second one. The DLL pointed by rational purify are MFC DLLs (inserting the screen shot below)

    Is this a known issue with the MFC DLL or am i doing something wrong?

    I have a server application where i have to create CDatabase object multiple times and i end up leaking a lot of memory over a period of time.

    I can provide more information about this issue if required. Thanks in advance.

    • Moved by Shu 2017 Monday, February 9, 2015 6:35 AM third party product
    Thursday, February 5, 2015 7:15 AM

All replies

  • There are couple of error

    1) Prior deleting your database pointer you never tested whether it is NULL or not.

    2)You called close before even checking whether your open request succeeded

    3)What if you are getting some other exception apart from CDBException then your memory is never getting free in first example

    I think you should reconsider your program write it properly and then perform your comparison.

    Thanks


    Rupesh Shukla

    Thursday, February 5, 2015 6:17 PM
  • Hi Rupesh,

    Including your comments in my code has no affect on the memory leaks, they still persist, because this is not the underlying cause for the leaks.

    this simple piece of code below will also leak memory according to rational purify, but to observe the leaks in windows task manager you will have to run this in a loop, as i demonstrated in my previous sample..

    Assume that my OpenEx is always successful as i am running the database locally.

    {
        	CDatabase *db = new CDatabase;
        	
    	BOOL bRes = db->OpenEx(_T("DSN=MyData;UID=anon;PWD=pass"));
        	db->Close();
        	
        	delete db;
        	db=NULL;
    }
    Thanks


    Thanks

    Thursday, February 5, 2015 7:43 PM
  • I am trying to reproduce this issue on my side, but it seem there is no memory leak in my simple sample. I use _CrtDumpMemoryLeaks() to test the memory leak.

    https://msdn.microsoft.com/en-us/library/d41t22sb.aspx

    If I comment this line: db->Close();
    I can detect the memory leak at CDatabase *db = new CDatabaseSee the screenshot.

    If follow your sample code, there is memory leak message in the output view pane.

    #define CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <crtdbg.h>
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #endif

    .....

    int CMFCCdbMLTestApp::ExitInstance() { // TODO: Add your specialized code here and/or call the base class _CrtDumpMemoryLeaks(); return CWinApp::ExitInstance(); }


    May you can use some use tool like WinDbg tool to find more information about this issue. 


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 6, 2015 6:37 AM
  • Hi Shu Hu,

    Thanks for taking out your time for this.

    I guess you tried running the sample with MSACCESS as the database, after your post i tried with MSACCESS too and there were no memory leaks!!

    But..

    If you run the same code with Oracle database then you will surely see the leaks that i am complaining about.

    I am using Oracle 11g database with ODBC drivers provided by Oracle for my client (Driver Ver. 11.02.00.03). I am not using Microsoft ODBC for Oracle drivers.

    Friday, February 6, 2015 9:10 AM
  • Oracle is  a third party product, If this issue do not appear in Microsoft database with Microsoft ODBC, it would be better to ask for support from Oracle.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 9, 2015 6:34 AM