CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2006
    Posts
    53

    memory leak problem

    That's the task (should use copy by value semantics):
    Code:
    typedef struct {
      // street address
      char street[30];
      // postal code
      int postalCode;
    } Address;
    
    /*
     * This function creates a new address with a certain street address
     * and a certain postal code.
     */
    Address addressConstruct(const char *street, int postalCode);
    // Accessor for the street address. The caller
    // of this function must free() the return value when
    // he/she is done with it!
    char *addressGetStreet(Address addr);
    Here is my implementation
    Code:
    Address addressConstruct(const char *street, int postalCode)
    {
    	Address add;
    
    	strcpy(add.street, street);
    	add.postalCode = postalCode;
    
    	return add;
    }
    char *addressGetStreet(Address addr)
    {
    	char* ch;
    	
    	ch = (char*)malloc((strlen(addr.street)+1)*sizeof(char));
    	strcpy(ch, addr.street);
    	
    	return ch;
    }
    Could someone tell me how to use DevC++ to detect memory leaks?
    Thanks in advance.

  2. #2
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: memory leak problem

    Hi,

    You can use memory validator software tool for detecting memory leaks....
    http://www.softwareverify.com/cpp/memory/index.html

    also you can create wrapper for Malloc() and free and by keeping track of memory allocation. you can detect memory leaks.

    -Anant
    "Devise the simplest possible solution that solves the problems"

  3. #3
    Join Date
    Dec 2006
    Posts
    53

    Re: memory leak problem

    Quote Originally Posted by anantwakode
    http://www.softwareverify.com/cpp/memory/index.html[/url]
    -Anant
    the link does not work.

    Actually function "addressGetStreet" creates memory leaking. I guess I didn't understand correctly the following text:
    Code:
    // Accessor for the street address. The caller
    // of this function must free() the return value when
    // he/she is done with it!
    Could someone explain what does it mean?

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

    Re: memory leak problem

    Quote Originally Posted by coandrei
    the link does not work.

    Actually function "addressGetStreet" creates memory leaking. I guess I didn't understand correctly the following text:
    Code:
    // Accessor for the street address. The caller
    // of this function must free() the return value when
    // he/she is done with it!
    Could someone explain what does it mean?
    It means what it says -- you called malloc() and returned the pointer. So who will call free()?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Feb 2005
    Location
    Pune (India)
    Posts
    644

    Thumbs up Re: memory leak problem

    Quote Originally Posted by coandrei
    the link does not work.

    Actually function "addressGetStreet" creates memory leaking. I guess I didn't understand correctly the following text:
    Code:
    // Accessor for the street address. The caller
    // of this function must free() the return value when
    // he/she is done with it!
    Could someone explain what does it mean?
    Link is working now......there must be some server problem I cheked it.

    in your shown code actually there is no memory leak....
    addressGetStreet() function allocates memory and return pointer...it means its the responcibility of the caller to deallocate the memory. or you can do is return the ref to address string array.....


    -Anant
    "Devise the simplest possible solution that solves the problems"

  6. #6
    Join Date
    Dec 2006
    Posts
    53

    Re: memory leak problem

    Quote Originally Posted by anantwakode
    Link is working now......there must be some server problem I cheked it.

    in your shown code actually there is no memory leak....
    addressGetStreet() function allocates memory and return pointer...it means its the responcibility of the caller to deallocate the memory. or you can do is return the ref to address string array.....


    -Anant
    I solved it now. The problem was that I was using directly the function in a printf() statement similar to:
    Code:
    printf("%s\n", addressGetStreet(a1));
    instead of
    Code:
    char *street;
    street = addressGetStreet(a1);
    printf("%s\n", street);
    free(street);
    Thank you for explanations

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

    Re: memory leak problem

    Quote Originally Posted by coandrei
    I solved it now. The problem was that I was using directly the function in a printf() statement similar to:
    Code:
    printf("%s\n", addressGetStreet(a1));
    instead of
    Code:
    char *street;
    street = addressGetStreet(a1);
    printf("%s\n", street);
    free(street);
    Thank you for explanations
    Sorry, but you didn't fix the problem.

    If the function returns a char*, it is perfectly acceptable in a printf() function that accepts a "%s" format specifier. All you really did was move the code around a little bit, thereby moving the bug to another area of your program.

    So as an analogy, instead of adding 2 + 2, you added 1 + 3. They both still equal 4. Different numbers, same results.

    Regards,

    Paul McKenzie

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

    Re: memory leak problem

    So you need to change your code back to where it was crashing, and really fix the problem. The other way is just masking the bug, and you don't know when the bug will appear again.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: memory leak problem

    Quote Originally Posted by Paul McKenzie
    If the function returns a char*, it is perfectly acceptable in a printf() function that accepts a "%s" format specifier.
    I have to disagree. Using such a function ( one that allocates memory that the caller has to deallocate ) in a printf() statement will always result in a memory leak.
    coandrei's solution is the only way to use such a function in a printf() statement.
    Kurt

  10. #10
    Join Date
    Sep 2005
    Location
    China
    Posts
    21

    Re: memory leak problem

    Why don't you change your routine to avoid "malloc" ?

    Code:
    char *addressGetStreet(Address addr)
    {
    	return (char*)addr.street;;
    }
    I'm somewhere in the world.

  11. #11
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: memory leak problem

    Quote Originally Posted by vlongsoft
    Why don't you change your routine to avoid "malloc" ?
    • It introduces a life time issue: The string becomes invalid as soon as the Address object dies... Actually, your code is buggy because Address is passed by value, so the pointer is invalid as soon as the addressGetStreet function is exited.
    • It breaks encapsulation, for several reasons.
      • If, for some reason, the street string has to be computed on demand, it won't work properly unless a cache (which may use too much memory) is used.
      • It isn't clear whether modifying (if it's possible) the street of an address, can modify existing references to this street, or not.
        If you require that the street reference returned by addressGetStreet becomes a "mirror" of the street string inside the structure, then, encapsulation is obviously very flawed, as even a simple implementation change, such as a resizable malloc'ed buffer, would be impossible.
      • Giving a non-const access to an internal buffer, means that the user can modify the string inside your structure without any control. So, for example, you won't be able to keep track of the length of the string.
        This last one can easily be corrected. Replacing "char*" with "const char*".

      Conclusion: Encapsulation is broken to such an extent, that it's merely equivalent to a public array member!
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

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