Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Thread, CString function and memory leak

Thread, CString function and memory leak

Scheduled Pinned Locked Moved C / C++ / MFC
helpperformancetutorialquestion
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J B 0
    wrote on last edited by
    #1

    Hi guys, I'm having this memory leak problem I do know how to resolve it. Please help. The program creates a thread and the thread has to use some class members of its belonging class. So I pass the class' pointer to it, when create the thread in the main class CMyDlg:

    AfxBeginThread(doThreadProc, this);
    

    In particular, I need to access this class member function, which will return CString value:

    CString CMyDlg::ProcessString()
    {
    	CString resString;
    
    	// intialise and manipulate resString
    	............
    
    	return resString;
    }
    

    In the thread's function, I use this ProcessString() function.

    UINT CMyDlg::doThreadProc(LPVOID param)
    {
    	CMyDlg *pMyDlg = (CMyDlg *)param;
    
    	CString myString;
    
    	myString.SetString(pMyDlg->ProcessLabel());
    
    	......
    }
    

    As it shows I get a memory leak if I do this ProcessString() function in the thread. I won't get that if the function is run in the main class (process). Also, I notice there is also no leak if resString is only declared but never being accessed/manipulated within the function call. In other words, when the ProcessString() function is called by the thread, memory leak will happen if the CString variable (declared in the function) that is manipulated, is the one used to return the function value. Any ideas? Thanks alot~

    V A 2 Replies Last reply
    0
    • J J B 0

      Hi guys, I'm having this memory leak problem I do know how to resolve it. Please help. The program creates a thread and the thread has to use some class members of its belonging class. So I pass the class' pointer to it, when create the thread in the main class CMyDlg:

      AfxBeginThread(doThreadProc, this);
      

      In particular, I need to access this class member function, which will return CString value:

      CString CMyDlg::ProcessString()
      {
      	CString resString;
      
      	// intialise and manipulate resString
      	............
      
      	return resString;
      }
      

      In the thread's function, I use this ProcessString() function.

      UINT CMyDlg::doThreadProc(LPVOID param)
      {
      	CMyDlg *pMyDlg = (CMyDlg *)param;
      
      	CString myString;
      
      	myString.SetString(pMyDlg->ProcessLabel());
      
      	......
      }
      

      As it shows I get a memory leak if I do this ProcessString() function in the thread. I won't get that if the function is run in the main class (process). Also, I notice there is also no leak if resString is only declared but never being accessed/manipulated within the function call. In other words, when the ProcessString() function is called by the thread, memory leak will happen if the CString variable (declared in the function) that is manipulated, is the one used to return the function value. Any ideas? Thanks alot~

      V Offline
      V Offline
      vikramlinux
      wrote on last edited by
      #2

      hi, CString CMyDlg::ProcessString(){ CString resString; // intialise and manipulate resString ............ return resString; } I think u should not return address of local variable.

      A J 2 Replies Last reply
      0
      • J J B 0

        Hi guys, I'm having this memory leak problem I do know how to resolve it. Please help. The program creates a thread and the thread has to use some class members of its belonging class. So I pass the class' pointer to it, when create the thread in the main class CMyDlg:

        AfxBeginThread(doThreadProc, this);
        

        In particular, I need to access this class member function, which will return CString value:

        CString CMyDlg::ProcessString()
        {
        	CString resString;
        
        	// intialise and manipulate resString
        	............
        
        	return resString;
        }
        

        In the thread's function, I use this ProcessString() function.

        UINT CMyDlg::doThreadProc(LPVOID param)
        {
        	CMyDlg *pMyDlg = (CMyDlg *)param;
        
        	CString myString;
        
        	myString.SetString(pMyDlg->ProcessLabel());
        
        	......
        }
        

        As it shows I get a memory leak if I do this ProcessString() function in the thread. I won't get that if the function is run in the main class (process). Also, I notice there is also no leak if resString is only declared but never being accessed/manipulated within the function call. In other words, when the ProcessString() function is called by the thread, memory leak will happen if the CString variable (declared in the function) that is manipulated, is the one used to return the function value. Any ideas? Thanks alot~

        A Offline
        A Offline
        Antony M Kancidrowski
        wrote on last edited by
        #3

        If you change

        CString myString;
        myString.SetString(pMyDlg->ProcessLabel());

        to

        CString myString(pMyDlg->ProcessLabel());

        or

        CString myString;
        myString = pMyDlg->ProcessLabel();

        you should not get memory leaks. Ant.

        J 1 Reply Last reply
        0
        • V vikramlinux

          hi, CString CMyDlg::ProcessString(){ CString resString; // intialise and manipulate resString ............ return resString; } I think u should not return address of local variable.

          A Offline
          A Offline
          Antony M Kancidrowski
          wrote on last edited by
          #4

          This does not return the address of the local variable it returns a copy Ant.

          1 Reply Last reply
          0
          • V vikramlinux

            hi, CString CMyDlg::ProcessString(){ CString resString; // intialise and manipulate resString ............ return resString; } I think u should not return address of local variable.

            J Offline
            J Offline
            J B 0
            wrote on last edited by
            #5

            Thanks vikrams for the reply, I see what you are getting. I've always thought that CString is different to PCTSTR or char *. When a function is declared to have return value type CString, it gets its own memory allocation, instead of a pointer to some memory address. So when, in my case, resString goes out of scope upon the function return, the function value still is there (been copied over from resString), in its own space. I'm not sure if I'm right on this point, correct me if I'm wrong. If I were wrong, I think this would only lead to the loss of data, instead of memory leak. I tried anyway, placing resString as a class member in .h file or as a global variable on the top of .cpp still gives memory leaks :( Any more ideas? BTW: the CMyDlg class is actually a child dialog of some main dialog. It is created in the main dialog's OnInitDialog()

            	pMyDlg = new CMyDlg();
            	pMyDlg->Create(IDD_MY_DIALOG, this);
            	pMyDlg->ShowWindow(SW_SHOW);	// main dialog is shown by default
            

            and destroyed in the main dialog's OnDestroy()

            	pMyDlg->DestroyWindow();
            	delete pMyDlg;
            

            if that has something to do with it. Thanks again.

            1 Reply Last reply
            0
            • A Antony M Kancidrowski

              If you change

              CString myString;
              myString.SetString(pMyDlg->ProcessLabel());

              to

              CString myString(pMyDlg->ProcessLabel());

              or

              CString myString;
              myString = pMyDlg->ProcessLabel();

              you should not get memory leaks. Ant.

              J Offline
              J Offline
              J B 0
              wrote on last edited by
              #6

              Thanks Antony, I tried both of them, didn't stop the memory leak :( The problem didn't not seem to be on myString, as long as pMyDlg->ProcessLabel() is called in the thread (without saving the return value to anywhere), the memory leak will occur.

              A 1 Reply Last reply
              0
              • J J B 0

                Thanks Antony, I tried both of them, didn't stop the memory leak :( The problem didn't not seem to be on myString, as long as pMyDlg->ProcessLabel() is called in the thread (without saving the return value to anywhere), the memory leak will occur.

                A Offline
                A Offline
                Antony M Kancidrowski
                wrote on last edited by
                #7

                What do you do in your ProcessLabel() function. It isn't the return of the copied string that is the problem. Ant.

                J 1 Reply Last reply
                0
                • A Antony M Kancidrowski

                  What do you do in your ProcessLabel() function. It isn't the return of the copied string that is the problem. Ant.

                  J Offline
                  J Offline
                  J B 0
                  wrote on last edited by
                  #8

                  I was actually narrowing the possibility by having ProcessString() to look like below:

                  CString CMyDlg::ProcessString()
                  {
                  	CString resString;
                  	
                  	resString += "";
                  
                  	return resString;
                  }
                  

                  Commenting out resString += ""; will stop the memory leak. Now, I go further and try replace with the following: resString = ""; <--- no memory leak resString.SetString(""); <--- no memory leak resString = " "; <--- memory leak So, I think as soon as I start putting stuff in resString or when not putting stuff, but use += (resString += ""), I get memory leak. I am really confused :confused: And when this function is called normally (e.g. not in a thread), there is no problem whatsoever. That's why I think it's something related to the way the function is called in the thread as well.

                  A 1 Reply Last reply
                  0
                  • J J B 0

                    I was actually narrowing the possibility by having ProcessString() to look like below:

                    CString CMyDlg::ProcessString()
                    {
                    	CString resString;
                    	
                    	resString += "";
                    
                    	return resString;
                    }
                    

                    Commenting out resString += ""; will stop the memory leak. Now, I go further and try replace with the following: resString = ""; <--- no memory leak resString.SetString(""); <--- no memory leak resString = " "; <--- memory leak So, I think as soon as I start putting stuff in resString or when not putting stuff, but use += (resString += ""), I get memory leak. I am really confused :confused: And when this function is called normally (e.g. not in a thread), there is no problem whatsoever. That's why I think it's something related to the way the function is called in the thread as well.

                    A Offline
                    A Offline
                    Antony M Kancidrowski
                    wrote on last edited by
                    #9

                    Um, Here is a thought what if you changed your code thus Add a member variable, and alter the return for ProcessString

                    virtual CString& ProcessString();

                    private:
                    CString m_resString;

                    Then alter the Process String to manipulate m_resString. Ant.

                    J 1 Reply Last reply
                    0
                    • A Antony M Kancidrowski

                      Um, Here is a thought what if you changed your code thus Add a member variable, and alter the return for ProcessString

                      virtual CString& ProcessString();

                      private:
                      CString m_resString;

                      Then alter the Process String to manipulate m_resString. Ant.

                      J Offline
                      J Offline
                      J B 0
                      wrote on last edited by
                      #10

                      Thanks again, Antony, unfortunately, that didn't seem to work. HOWEVER, instead of using CString &, I tried LPCTSTR in conjuction with the member variable, and the following code

                      m_resString += "";
                      

                      no longer gives memory leak (unlike before or ur suggested CString & declaration here). But if I start assigning values into the CString, it still generates memory leak. I am extremely confused now. Is there a resonable explanation here? Thanks

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups