Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory leak problem 6

Status
Not open for further replies.

Valius

Programmer
Oct 20, 2000
174
US
I've got a pointer declared like so:

char *SomeChar;

SomeChar = new char[10];

I then want to delete it before my program quits...so I do this:
SomeChar = 0;
delete SomeChar;

Is this correct to free up mem? If so, I think I know where the problem may lie. Thanks in advance!

Niky Williams
NTS Marketing
Niky.Williams@NTSMarketing.com
 
SomeChar = 0;//is wrong. You mustn't set
//pointer to 0 after deleting
delete SomeChar;//is wrong too
//because you delete one array of chars, not only
//a single character
correct is
delete[] SomeChar;
SomeChar = 0;//not required, but in this order correct
John Fill
1c.bmp


ivfmd@mail.md
 
AAAHHHH!!!...earlier I was trying
delete SomeChar[];
but that wasn't working. I had thought I had seen something like that. Awesome...thanks alot!

Niky Williams
 
Further on what JohnFill said, the problem was the sequence in which you executed it. By assigning an address of 0 to SomeChar, you'd effectively lost the address of the array that needed releasing. Delete first, then assign NULL.

Incidentally, the statement
Code:
delete
Code:
SomeChar;
will work, although it isn't the preferred method for releasing memory of an array.

The brackets instruct the compiler to call the destructor for each element of the array. Since char doesn't have a destructor, using the [] becomes optional. So either of these will work without a memory leak:
Code:
delete
Code:
 SomeChar;
SomeChar = 0;
Code:
// Or use SomeChar = NULL;
--OR--
Code:
delete
Code:
[] SomeChar;
SomeChar = 0;
 
It's when I used delete SomeChar that I got the memory leak problems. Using delete[] SomeChar worked fine without any memory leaks.

Niky Williams
 
In this regard, there is a common mistake that is made
when deleting a character array.

someone has,

char* SomeBuffer;
SomeBuffer = new char[100];

Now for some reason or the other, you manipulate the pointer directly instead of accessing it using an index.

say,
char c= SomeBuffer++;

Now

delete [] SomeBuffer; will cause a memory fault, since
the pointer doesnt point to where the memory was allotted.
You need to do,

SomeBuffer--;
delete [] SomeBuffer;

abp :cool:
 
That's cool ABP, it makes sense. Never thought of it that way before. I'll will most certainly keep that in mind. Thanks for everyones help!

Niky Williams
 
What I want to add if you do:
first variant:
char* xx;
xx=new char[100];
delete[] xx;
delete[] xx;//deleting not allocated -- memory fault
second variant:
char* xx;
xx=new char[100];
delete[] xx;
xx=NULL;
delete[] xx;//deleting null pointer, everything is fine
---------
the second variant is good to use in destructors John Fill
1c.bmp


ivfmd@mail.md
 
Why would you call "delete[] xx" twice? What does this accomplish?

Niky Williams
 
For exampleI do not know what the realisation of OneClass is. I do not know if the dtor free or not the variable parameter of some operations:
char* x;
char* y;
{//begin
OneClass xx(&x);//call the constructor
xx.SomeOperation(&y);
//supposing the ctor alloc memory to x

}//end scope, supposing destructor calls delete[] x;and y
delete[] x;
//I'm carefully and don't want to get memory
//leaks. If x is not reseted to NULL in xx's dtor
//the last delete is a memory fault.
delete[] y;// the same history John Fill
1c.bmp


ivfmd@mail.md
 
Hmm...I'm now having problems with something else along the same lines...here is what I have...

char **WIDTH_ARRAY;

WIDTH_ARRAY = new int*[15];

I can now allocate space by saying
WIDTH_ARRAY[X] = new int;


Now that we got that outta the way...here is how I'm deleting it at the end...

delete[] *WIDTH_ARRAY;
delete[] WIDTH_ARRAY;

I've tried many variation of setting the pointers to null and a few other things as well, but to no luck. I keep getting mem leaks. The reason I know that it is this particular varible is cause VC++ is giving me the address of where the mem leaks occur and according to VC++ those mem addresses are somewhere in the WIDTH_ARRAY variables; What am I doing wrong? Any help is greatly appreciated. Thanks in advance!!!

Niky Williams
NTS Marketing
Niky.Williams@NTSMarketing.com
 
Is the first statement (char **WIDTH_ARRAY) what's actually in your program? If so, that's what's causing your problem - it's deleting, in essence, new char's, when your actually allocating new int's. Try changing the declaration to int **WIDTH_ARRAY and see if that helps! (BTW, I could be way off base, but that jumped out at me! :) )
 
Doh'...I misstyped it...WIDTH_ARRAY is defined as an int, not a char...my mind is a little mushy today. Sorry.

Niky Williams
 
I think you should do the following:

for( int j = 0; j < 15; j++ )
{
delete WIDTH_ARRAY[j]; // WIDTH_ARRAY is an array of int pointers.
}

then

delete[] WIDTH_ARRAY;

Basically, the way I see it, you should always mirror-image the &quot;new&quot; and &quot;delete&quot; statements. In you case you wrote:

WIDTH_ARRAY[X] = new int;

then you should mirror that with the

delete WIDTH_ARRAY[j];

line I wrote above. If you say

new int*[15];
then use delete[], etc.

I hope I am right, and that it helps you!

Vincent
 
Awesome Vincent, that worked like a charm. What you say does make perfect sense. Thank you so much for your help!

Niky Williams
 
From which site i can get free software for finding memory leakage in c++ ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top