Solved

Proper deletion of wchar memroy in C++

Posted on 2010-08-20
3
375 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

932 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

13 Experts available now in Live!

Get 1:1 Help Now