CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2005
    Posts
    172

    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?

  2. #2
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    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

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Using delete [] inside a function

    Quote 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.

  4. #4
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Using delete [] inside a function

    Quote 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

  5. #5
    Join Date
    Apr 1999
    Posts
    50

    Re: Using delete [] inside a function

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured