Link to home
Start Free TrialLog in
Avatar of jsaxon2
jsaxon2

asked on

Memory Leak question

VC++ Debugger is showing a memory leak in my app.

Detected memory leaks!
Dumping objects ->
C:\Conversion\drupgrade\drupgradeView.cpp(1703) : {565} normal block at 0x00B06538, 100 bytes long.
 Data: < Z   [  ,]   ]  > A4 5A B0 00 AC 5B B0 00 2C 5D B0 00 84 5D B0 00

The code is as follows:

BOOL CDrupgradeView::AddThread(CString  server, CString db, CString strUsername, CString strPassword, CString strProc, CString strProcType, CString strProcID, UINT iStatus,     CString strName)
{
     CThreadParams *pSQLCmd = new CThreadParams; //Memory Leak
     pSQLCmd->strProcType = strProcType;
     pSQLCmd->strProc = strProc;
     pSQLCmd->strProcID = strProcID;
     pSQLCmd->strUsername = strUsername;
     pSQLCmd->strPassword = strPassword;
     pSQLCmd->strName = strName;
     pSQLCmd->db = db;
     pSQLCmd->server = server;

     CWinThread* pThread;
     HANDLE hTerminateSignal = CreateEvent(NULL, TRUE, FALSE, NULL);
     
     // FOR NOW: USE FIXED PRIORITY !!!
     pThread = AfxBeginThread(&StartingThreadFunction, pSQLCmd,
          THREAD_PRIORITY_NORMAL, 0, 0);
         
     if(!pThread)
     {
          AfxMessageBox(_T("Error strarting new adding thread!"));
          CloseHandle(hTerminateSignal);
                return false;
     }
     return true; //Thread Added
}


How can I correct this?
Avatar of jkr
jkr
Flag of Germany image

>>How can I correct this?

Have the thread

delete pSQLCmd;

before it exits.
Avatar of hide_in
hide_in

There two points.
1. please notice the code you wrote - "if (!pThread)"..
when the condition occurs, the prog returns without releasing the buffer pointed by pSQLCmd.
2.please ensure that you release the buffer you allocate here when your thread exits.
Avatar of jsaxon2

ASKER

I have added the delete pSQLCmd to the threads but the debugger is still reporting memory leaks. What else could I try?
ASKER CERTIFIED SOLUTION
Avatar of pzpn
pzpn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your pSqlParams are not being destroyed.  

To correct this,
1.  delete the params in the start code if thread not started.
2.  delete the params in the thread code before the thread completes

Your pThread is not being destroyed.
To correct this:
1.  Create the thread as suspended.
2.  If created, set it to auto delete
3.  Resume the thread.

This will also solve the problem of the thread possibly completing before it can be checked as a previous poster talkeed about.


You can solve this problem in 2 ways.
First Method

delete the parameter in the thread after processing is over.

Second method

1.Once the thread is created in main, make it wait for an object from the thread.
2.In the thread,copy the content of the parameter in a local variable.
3.Then, send signal to the main thread,
4.Delete the pSQLCmd in the main

Example

HANDLE h_Done = NULL;

BOOL CDrupgradeView::AddThread(CString  server, CString db, CString strUsername, CString strPassword,
CString strProc, CString strProcType, CString strProcID, UINT iStatus,     CString strName)
{
    CThreadParams *pSQLCmd = new CThreadParams; //Memory Leak
    pSQLCmd->strProcType = strProcType;
    pSQLCmd->strProc = strProc;
    pSQLCmd->strProcID = strProcID;
    pSQLCmd->strUsername = strUsername;
    pSQLCmd->strPassword = strPassword;
    pSQLCmd->strName = strName;
    pSQLCmd->db = db;
    pSQLCmd->server = server;

    CWinThread* pThread;
    HANDLE hTerminateSignal = CreateEvent(NULL, TRUE, FALSE, NULL);
   
    h_Done = CreateEvent(NULL, TRUE, FALSE, "CopyingDone");

    // FOR NOW: USE FIXED PRIORITY !!!
    pThread = AfxBeginThread(&StartingThreadFunction, pSQLCmd,
         THREAD_PRIORITY_NORMAL, 0, 0);
         
    if(!pThread)
    {
         AfxMessageBox(_T("Error strarting new adding thread!"));
         CloseHandle(hTerminateSignal);
         CloseHandle(h_Done);
         delete pSQLCmd;
               return false;
    }

  if (::WaitForSingleObject(h_Done, 10) == WAIT_OBJECT_0)
     delete pSQLCmd;
  return true; //Thread Added
}

Example code in the Thread function

UINT StartingThreadFunction( LPVOID pParam )
{
    CThreadParams SQLCmd;
     memcpy(&SQLCmd,pParam,sizeof(CThreadParams));
    SetEvent(h_Done);
    ::AfxMessageBox(_T("Done"));
    .    // Thread code
    .
    .
    return 0;
}