|
-
July 4th, 2006, 05:23 PM
#1
Using delete [] inside a function
Hi,
I have a quick question: I using dynamic string inside function and the value returned by the function is the string. the problem is, after running some tests, i have a heap problem, memory leak.
As far as i know any data used by function is deleted upon exit so there is no need to use "delete [] uncompr ;".
Here is the func. code: (ps: all g_* are globals)
Code:
Byte* UnCompress (Byte* compr){
int err;
Byte *uncompr = new Byte [g_uncomprLen];
err = uncompress(uncompr, &g_uncomprLen, compr, g_comprLen);
if (err!=Z_OK)
{
printf("Uncompress() failed : %d\n", err);
return 0;
} else {
return uncompr;
}
}
I am trying to eliminate options so tell me what you think, can it be my problem?
-
July 4th, 2006, 05:31 PM
#2
Re: Using delete [] inside a function
<<As far as i know any data used by function is deleted upon exit so there is no need to use "delete [] uncompr ;".>>
what makes you say that, ofcourse it doesnt. it cleans up memory thats allocated by the "compiler" what you dynamicly allocate is not cleaned up.
also for your problem use a string class
-
July 4th, 2006, 06:00 PM
#3
Re: Using delete [] inside a function
 Originally Posted by portnov
I have a quick question: I using dynamic string inside function
I don't see a dynamic string. What I see is a pointer to something called "Byte" being dynamically allocated by using new[].
and the value returned by the function is the string. the problem is, after running some tests, i have a heap problem, memory leak.
Of course you do, it is obvious.
1) If err != Z_OK, where do you delete [] the memory you allocated? All you do is print a message that the decompression failed, and then return, leaving a memory leak.
2) If the code doesn't take this path, when you return uncompress, who delete's this memory?
Here is a much safer version of what you want to do:
Code:
#include <vector>
//...
typedef std::vector<Byte> ByteData;
//...
ByteData UnCompress (const ByteData& compr)
{
int err;
ByteData uncompr(g_uncomprLen);
err = uncompress(&uncompr[0], &g_uncomprLen, &compr[0], g_comprLen);
if (err!=Z_OK)
{
printf("Uncompress() failed : %d\n", err);
return 0;
} else {
return uncompr;
}
}
Now there are no memory leaks. You can use ByteData as a dynamic array of Byte, just as if you used new Byte[], but even better and safer.
The bottom line is that whenever you use new[], you must use delete[].
Since doing all of this new[]/delete[] stuff is avoidable using proper container classes, the container class (in this case std::vector) would have been preferable.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 4th, 2006 at 06:03 PM.
-
July 4th, 2006, 07:01 PM
#4
Re: Using delete [] inside a function
 Originally Posted by portnov
As far as i know any data used by function is deleted upon exit so there is no need to use "delete [] uncompr ;".
Wrong, for every new you should do a delete.
- petter
-
July 5th, 2006, 05:40 AM
#5
Re: Using delete [] inside a function
 Originally Posted by portnov
As far as i know any data used by function is deleted upon exit so there is no need to use "delete [] uncompr ;".
Data allocated with new[] and accessed through a pointer is not deleted, but if, instead, you declare a vector:
Code:
std::vector<Byte> uncompr(g_uncomprLen);
your memory will be cleaned up for you.
Hope that helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|