Solved

Memory Leak question

Posted on 2002-04-22
6
976 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

13 Experts available now in Live!

Get 1:1 Help Now