Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CStringArray memory management

Posted on 1997-11-07
2
Medium Priority
?
921 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Accepted Solution

by:
wpinto earned 200 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

Wilfred
0
 

Author Comment

by:geneo
ID: 1172723
Thanks Wilfred.  I appreciate your time.  Gene.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

664 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