Solved

CString memory leak in cwinthread

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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.

756 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