Solved

CString memory leak in cwinthread

Posted on 2011-03-01
4
1,500 Views
Last Modified: 2012-05-11
When running my code I get this:

Detected memory leaks!
Dumping objects ->
strcore.cpp(118) : {86} normal block at 0x00302DF0, 31 bytes long.
 Data: <            Dude> 01 00 00 00 12 00 00 00 12 00 00 00 44 75 64 65
Object dump complete.

This is the code:
CString sD;
sD = "Dude this is weird";

If I comment out the second line only, the memory leak goes away.

This code is within a CWinThread object that is called from the main window and exits with a call to AfxEndThread(0); after sending a message back to the parent.  I can put this code in the parent window and it's fine.

There's another instance within the CWinThread object prior to these lines where this code is run:

CFtpConnection* pCon = NULL;
                     
try
      {
           pCon = parent->m_sSession->GetFtpConnection(FTP_SERVER, FTP_USER, FTP_PW, 21, TRUE);
      }
      catch (CInternetException* pEx)
      {
            CString sConnectionErrorMsg;
            TCHAR sz[1024];
            pEx->GetErrorMessage(sz, 1024);
            sConnectionErrorMsg.Format("Return error message: %s", sz);
            returnCode = UPDATE_CONNECTION_ERROR;
            m_sLog += "\r\nFTP connection failed\r\n" + sConnectionErrorMsg;
            pEx->Delete();
            delete pCon;
            pCon = NULL;
      }

Notice the use of CString object sConnectionErrorMsg here.  This does NOT create a memory leak.

Any ideas?
0
Comment
Question by:theclay
[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
4 Comments
 
LVL 4

Accepted Solution

by:
parnasso earned 350 total points
ID: 35007545
My guess is that in the first case, when you end the thread with AfxEndThread(0), no destructor of the CString is called.

On the other hand, the code you written of the thread that uses the CString object sConnectionErrorMsg, DOES call the CString destructor and therefore, there is no memory leaks.

To solve your problem, wrap the first thread code in a function, and outside of that function called AfxEndThread so that every destructor get called because the objects get out of the scope.

Something like this.

functionDosomething()
{
  CString sD;
  sD = "Dude this is weird";
} // The object sD get out of scope so it will be released

functionThread()
{
   // do something
  functionDosomething();
  AfxEndThread(0);
}

Hope this helps
0
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 150 total points
ID: 35007909
Hi,

I agree with parnasso - just as additional info: 'AfxEndThread' internally uses '_endthreadex'. http://msdn.microsoft.com/en-us/library/hw264s73%28v=VS.100%29.aspx says: '_endthread and _endthreadex cause C++ destructors pending in the thread not to be called'

ZOPPO
0
 
LVL 34

Expert Comment

by:sarabande
ID: 35008873
you should omit the AfxEndThread. simply return from thread function.

Sara
0
 

Author Closing Comment

by:theclay
ID: 35009175
That took care of it - thanks.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

626 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