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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString multiple assignment leak?

CString multiple assignment leak?

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
6 Posts 4 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.
  • T Offline
    T Offline
    TechAvtar
    wrote on last edited by
    #1

    Hi, Do the following will cause memory leak? CString temp; //global variable temp = "hello"; //Assignment 1 temp = "world"; //Assignment 2 What will happen to the memory allocated for "hello"? Will it be released at assignment of "world"? Thanks JC

    L A J 3 Replies Last reply
    0
    • T TechAvtar

      Hi, Do the following will cause memory leak? CString temp; //global variable temp = "hello"; //Assignment 1 temp = "world"; //Assignment 2 What will happen to the memory allocated for "hello"? Will it be released at assignment of "world"? Thanks JC

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      TechAvtar wrote:

      What will happen to the memory allocated for "hello"? Will it be released at assignment of "world"?

      Yes. The current content of a CString variable will be automatically released when the variable goes out of scope, as well. [edit] Would someone like to explain why this has been 1-voted? The answer is correct. There may be some hedging involved as to whether new memory is actually allocated depending on the length of the two strings, as Joe pointed out, but the point is the reassignment of the variable will not result in a leak. THAT was the question. I assume the individual who voted it a 1 will now present some proof indicating that a leak WILL occur, justifying the 1? [/edit]

      L u n a t i c F r i n g e

      1 Reply Last reply
      0
      • T TechAvtar

        Hi, Do the following will cause memory leak? CString temp; //global variable temp = "hello"; //Assignment 1 temp = "world"; //Assignment 2 What will happen to the memory allocated for "hello"? Will it be released at assignment of "world"? Thanks JC

        A Offline
        A Offline
        Adam Roderick J
        wrote on last edited by
        #3

        CString is MFC class. And the buffer it uses inside this class is handled by CString, it has nothing to do with user unless you are creating a pointer of the CString( Which is not at a good practice.) Inside this CString class, destructor is having the code to delete the memory allocated for its internal buffer. So dont worrry, CString will delete that memory for you when it goes out of scope :)

        Величие не Бога может быть недооценена.

        modified on Monday, April 5, 2010 3:06 AM

        T 1 Reply Last reply
        0
        • A Adam Roderick J

          CString is MFC class. And the buffer it uses inside this class is handled by CString, it has nothing to do with user unless you are creating a pointer of the CString( Which is not at a good practice.) Inside this CString class, destructor is having the code to delete the memory allocated for its internal buffer. So dont worrry, CString will delete that memory for you when it goes out of scope :)

          Величие не Бога может быть недооценена.

          modified on Monday, April 5, 2010 3:06 AM

          T Offline
          T Offline
          TechAvtar
          wrote on last edited by
          #4

          But what if the usage is as below? The variable temp is not going out of scope. Will it cause leak? CString temp; //global variable while(1) { temp = "hello"; //Assignment 1 temp = "world"; //Assignment 2 }

          A 1 Reply Last reply
          0
          • T TechAvtar

            But what if the usage is as below? The variable temp is not going out of scope. Will it cause leak? CString temp; //global variable while(1) { temp = "hello"; //Assignment 1 temp = "world"; //Assignment 2 }

            A Offline
            A Offline
            Adam Roderick J
            wrote on last edited by
            #5

            It will release the memory of previously allocated pointer on the = operator of CString. See the code of CString. The specified function is called inside the = operator.

            void CString::AllocBeforeWrite(int nLen)
            {
            if (GetData()->nRefs > 1 || nLen > GetData()->nAllocLength)
            {
            // This release will release all the memory allocated previously, so you wont face any memory leak.
            Release();
            AllocBuffer(nLen);
            }
            ASSERT(GetData()->nRefs <= 1);
            }

            Hope you are now clear with it :)

            Величие не Бога может быть недооценена.

            modified on Monday, April 5, 2010 3:53 AM

            1 Reply Last reply
            0
            • T TechAvtar

              Hi, Do the following will cause memory leak? CString temp; //global variable temp = "hello"; //Assignment 1 temp = "world"; //Assignment 2 What will happen to the memory allocated for "hello"? Will it be released at assignment of "world"? Thanks JC

              J Offline
              J Offline
              Joe Woodbury
              wrote on last edited by
              #6

              Since "world" is the same length as "hello", no reallocation will be done; "world" will just overwrite "hello". If you set a string "hello world", the internal buffer would be reallocated in a safe manner, resulting in no leak. (For optimization reasons, CString sometimes allocates a larger buffer than requested, so even setting a larger string won't result in a reallocation.)

              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