Solved

Memory Leak question

Posted on 2002-04-22
6
978 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

8 Experts available now in Live!

Get 1:1 Help Now