Thread: Memory Leak??

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    37

    Memory Leak??

    Hello everybody
    I would appreciate if someone can help me with the following

    I am writing a program and debugging it, and suddenly when looking at the code I wrote, a doubt came to my mind: am I provoking a memory leak??

    Here is the code
    Code:
    someType * Ptr=0;
    
    for(i=0;i<3;i++)
    {
        Ptr=0;    // POINT 1
        // Do something 
     
       do{
          Ptr=someFunction();
        } (while Ptr==0);
      
       // POINT 2
       // Do something else
    }
    As you can see first I have some pointer Ptr pointing to 0.
    Then at the start of the loop (Point 1) I do that again.
    Then I enter a do-while loop in which a function someFunction produces a pointer which is given to Ptr. This contains some data. The do-while is to make sure that someFunction produces an actual data and not 0.
    Then I do something with the data, and go back to the start of the for loop.

    to start again I point this Ptr to 0

    and repeat everything again

    My question is, between this point 2 and the point 1, am I losing the produced data and producing a Memory Leak???

    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What does someFunction do and return?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Are you losing the data? Yes, you changed where Ptr points without saving the old value.

    Are you causing a memory leak? Who knows. As laserlight pointed out, we need to know what someFunction does and returns.

    A memory leak occurs when you allocate memory via malloc/calloc/realloc, and you dont' free() it. This includes failing to free memory allocated by a library or other code, which does a malloc under the hood.

    You're not the only person who has suffered from actual, or worried about potential memory leaks. There are great tools like Valgrind (for Linux) that help you find memory leaks. I don't know what the Windows tools are, but I know several exist.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    37
    Thank you very much for your reply both of you.

    Right now, someFunction is a function of this form
    Code:
    someType * someFunction(void)
    {
       static someType returnValue;
    
    // Here do some processing
    
       returnValue.firstComponent= someData1;
       returnValue.secondComponent = someData2;
    
      return &returnValue; 
    
    }
    Since this is a static variable I guess it stays even after leaving the function and since I am not using new or malloc, there is no risk for a memory leak. am I right?

    Just for theoretical study, what would you do if you dont use a static variable? If I instantiate a variable inside the function, and then I do what I posted in my first post, that surely will give a memory leak, wont it?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you "instantiate" a variable in someFunction (i.e. declare it as non-static; see example below) and return a pointer to it, you end up with undefined behavior. When the function returns, the memory that variable occupied becomes invalid (until used by something else), so the pointer you have to it points to invalid memory. Accessing invalid memory results in undefined behavior. In fact, modern compilers often catch this and warn you about it.
    Code:
    someType *someFunction(void)
    {
        someType returnValue;
    
        returnValue.firstComponent = someData1;
        returnValue.firstComponent = someData1;
    
        return &returnValue;
    }
    However, if you declare a pointer inside someFunction, allocate memory to that and return the value, then that's not undefined behavior, but it would cause a memory leak.
    Code:
    someType *someFunction(void)
    {
        someType * returnValue;
        ...
        return returnValue;  // No & here, returnValue is already a pointer.
    }
    At POINT 1 (Ptr=0, you overwrite the old value of Ptr (address of memory allocated in someFunction), so you no longer have that address to free the memory later. This is not a problem on the first iteration, Ptr doesn't point to allocated memory yet. Either free the memory at the end of the for loop, or save the address somewhere to be freed later.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KansaiRobot
    Since this is a static variable I guess it stays even after leaving the function and since I am not using new or malloc, there is no risk for a memory leak. am I right?
    Yes, but keep in mind that the caller must remember to copy the object if someFunction is called more than once and yet the caller wishes to use the value of the object from a prior call.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    37
    Thank you very much both. That was a good explanation

    So, just to be clear, now, by using a static variable, I am free of the memory leak problem and at the same time it is a valid piece of memory, is that correct?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible Memory Leak?
    By g1i7ch in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 01:35 PM
  2. Memory Leak
    By jtullo in forum C Programming
    Replies: 7
    Last Post: 12-11-2006, 11:45 PM
  3. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  4. memory leak
    By stormbringer in forum C Programming
    Replies: 7
    Last Post: 07-17-2002, 09:56 AM
  5. Memory Leak or not?
    By Eber Kain in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 01:05 PM

Tags for this Thread