Solved

CStringArray memory management

Posted on 1997-11-07
2
882 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 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

Wilfred
0
 

Author Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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