Link to home
Start Free TrialLog in
Avatar of theclay
theclay

asked on

CString memory leak in cwinthread

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?
ASKER CERTIFIED SOLUTION
Avatar of parnasso
parnasso
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you should omit the AfxEndThread. simply return from thread function.

Sara
Avatar of theclay
theclay

ASKER

That took care of it - thanks.