Solved

CStringArray memory management

Posted on 1997-11-07
2
836 Views
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.FreeExtra();
      pa.Empty(); // empty the original element at 'index'
}
0
Comment
Question by:geneo
2 Comments
 
LVL 2

Accepted Solution

by:
wpinto earned 50 total points
Comment Utility
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

Wilfred
0
 

Author Comment

by:geneo
Comment Utility
Thanks Wilfred.  I appreciate your time.  Gene.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now