CStringArray memory management

Posted on 1997-11-07
Last Modified: 2008-10-12
The following routine manages a global CStringArray, Message.  It simply replaces the CString at the specified index in Message with the new CString, string.  My question is this - How should I correctly delete the old CString in Message which is being replaced?  Does this code completely free up the old CString when this routine is exited?  Is there a better, correct way of doing it?

CStringArray Message;

void SetMessage(short index, CString string)
      CString pa;

      pa = Message.GetAt(index); // get old CString
      Message.SetAt(index,string); // insert new CString
      pa.Empty(); // empty the original element at 'index'
Question by:geneo

Accepted Solution

wpinto earned 50 total points
ID: 1172722
Hi geneo,

You don't have to worry about memory management when it comes to CStringArray or CStrings. MFC handles it internally for you.

if you don't need the previously stored string you could rewrite the SetMessage() function as given below

void SetMessage(short index, CString string)
  Message.SetAt(index,string); // insert new CString

and it would work fine (no memory leaks)

if you wish to access the prviously stored string try
void SetMessage(short index, CString string)
  CString pa;
  pa = Message.GetAt(index); // get old CString
  Message.SetAt(index,string); // insert new CString

You don't need the following statement pa.Empty(). The CString destructor automatically frees the space.

So don't worry, use it as would use normal integer array!

Hope this helps


Author Comment

ID: 1172723
Thanks Wilfred.  I appreciate your time.  Gene.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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
Handle Exceptions during instantiation 28 423
thread-safe code in c++ 2 116
GUI: DIalog Stacking and Popping in MS C++ 4 82
Exception thrown at 0x00007FFD5BC81F28 7 50
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

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