Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CString memory leak in cwinthread

Posted on 2011-03-01
4
Medium Priority
?
1,511 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 1400 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 600 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 35

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Introduction to Processes

722 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