Solved

Memory Leak question

Posted on 2002-04-22
6
977 Views
Last Modified: 2007-12-19
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?
0
Comment
Question by:jsaxon2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6961719
>>How can I correct this?

Have the thread

delete pSQLCmd;

before it exits.
0
 
LVL 1

Expert Comment

by:hide_in
ID: 6961722
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.
0
 
LVL 3

Author Comment

by:jsaxon2
ID: 6961820
I have added the delete pSQLCmd to the threads but the debugger is still reporting memory leaks. What else could I try?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Accepted Solution

by:
pzpn earned 100 total points
ID: 6962237
Have you added a delete before each return statement? i.e. at each point where the method could return you need to delete the memory allocated to pSQLCmd.
0
 
LVL 3

Expert Comment

by:danny_pav
ID: 6962847
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.


0
 

Expert Comment

by:hemaarul
ID: 6967851
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;
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now