Solved

CString memory leak in cwinthread

Posted on 2011-03-01
4
1,471 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
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 30

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 32

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

758 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

18 Experts available now in Live!

Get 1:1 Help Now