Solved

Proper deletion of wchar memroy in C++

Posted on 2010-08-20
3
380 Views
Last Modified: 2012-06-27
Team,

In my program function, I am creating three wchar_t arrays posted below and deleteing them at the end of the function before the function ends, but even after deletion it does not release the memory and continues to eat it up. Can someone tell me what I am doing wrong:

Creation:

wchar_t* char1 = new wchar_t[5000];
wchar_t* char2 = new wchar_t[5000];
wchar_t* char3 = new wchar_t[5000];

Deletion:
char1 = NULL;
char2 = NULL;
char3= NULL;
delete [] char1;
delete [] char2;
delete [] char3;
0
Comment
Question by:tr57285
  • 2
3 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 33487868
Remove these 3 lines:
char1 = NULL;
char2 = NULL;
char3= NULL;
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 33487891
You want to delete the memory region associated with the new.

wchar_t* char1 = new wchar_t[5000];
delete [] char1;

If the delete operator gets a NULL value to operate on, then it will do nothing. But in the above two lines, the delete operator gets the actual address associated with the new operator, and so will be able to properly deallocate the space created by the new operator.
0
 

Author Closing Comment

by:tr57285
ID: 33487957
Awesome!

Thanks
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 63
Problem to show menu 5 88
a reliable reference for c+11? 3 68
IIS Log files on Exchange 2013 server 6 119
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

810 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