Solved

Memory Leak question

Posted on 2002-04-22
6
980 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

717 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