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
Solved

CString memory leak in cwinthread

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

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
T-SQL:  Sigh---Boy, this is fun.... 12 57
ejb wildfly example 2 25
How to Convert a Quote to an Order in SalesForce 2 81
Selenium docs api java index 3 48
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

828 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