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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory Leaks and Delete not working ?

Status
Not open for further replies.

Kalisto

Programmer
Feb 18, 2003
997
GB
I ran my code and counted 10 memory leaks. The first one I spotted was that I was allocating a pointer to a char[], and not deleting it.

So at the end of the function I added

pChar = NULL;
delete[] pChar;

But I still have my memory leak at this point. Anyone comment why ?

K
 
ever* p = 0; delete p; do nothing if ptr == 0.
If you allocate char* p = new char[n]; then you must write delete [] p;
Do not delete via pointers allocated with malloc() and do not free mem allocated with new.
 
>ArkM
be a bit more attentive
>Kalisto
do not set pChar to null before deleting, change any deleting operations to:

delete[] pChar;
pChar = NULL;


Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Cheers, but if I don't make it null first, then I get an exception thrown via dbgheap.c

K
 
in this case, I think you try to delete a noninitialized variable, or the delete[] opperator on this buffer is called in some other places also. Take a look at the destructors you use. There could be also a problem if you pass your string to some functions or objects which are not yours, but they delete your buffer after you have done it already.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
I have watched in debug, the function that deletes it creates it. It still has a valid value until it is deleted.

I have removed all uses of it in the code, so it now goes

char mArr[5];
char* pArr = new char;
pArr = &mArr[0];

delete[] pArr;
pArr = NULL;

The NULL was before the delete, to ensure that delete did not affect the array.
 
1) new shall have a corresponding delete

2) new Something[] shall have a corresponding delete[]

Your new char creates 1 new char, not an array, while your delete [] attempts to delete an array.

> to ensure that delete did not affect the array.

But you want the delete to affect the array, why else would you call it? The problem is you dont have an array to delete, only a single little char.


/Per

if (typos) cout << &quot;My fingers are faster than my brain. Sorry for the typos.&quot;;
 
char mArr[5]; is a constant array and you can not delete it directly nor indirectly.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
by the way,
char mArr[5]; is not an allocation, and not a memory leak if you do not delete it.

char mArr[5];
char* pArr = new char;
pArr = &mArr[0];//the memory leak is there
//because you loose the old value or pArr.

change it to:
pArr[0] = mArr[0];

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Doh, thanks guys.

It's been one of those weeks, so I think I'll just give up and go home!

Cheers,
K
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top