Thread: C Memory Leak

Page 1 of 2 12 LastLast
Results 1 to 10 of 13
  1. #1 C Memory Leak 
    Registered Member

    Join Date
    Dec 2011
    Posts
    1,952
    Thanks given
    488
    Thanks received
    530
    Rep Power
    364
    Never mind. It was fixed thanks to @Graham and @saifix.
    Need Cheap & HQ Promotional Videos & Trailers?
    Feel free to visit my shop by clicking [Only registered and activated users can see links. ]
    Reply With Quote  
     

  2. #2  
    Registered Member
    Tommeh's Avatar
    Join Date
    Nov 2015
    Age
    18
    Posts
    1,478
    Thanks given
    217
    Thanks received
    583
    Rep Power
    2440
    if i had to guess it's probably that the while loop is infinite
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Programmer, Veteran and Respected Member



    Join Date
    Mar 2007
    Posts
    5,023
    Thanks given
    2,598
    Thanks received
    3,395
    Rep Power
    5000
    free(length) wouldn't make sense because it's not a pointer.

    You should be calling free(package) after every iteration of the loop - were you calling it only once?

    You also change the package pointer during the loop. You need to free() the exact value returned by malloc(), so keep a copy of that around. Calling free() with a pointer to the middle of the memory region that was allocated won't work - it needs to be given a pointer to the start.

    You're also missing an fclose() call at the end.

    btw, there's no need to cast the result of malloc. malloc returns a void pointer, and void pointers can be implicitly cast to any pointer type - there's no need for an explicit cast.
    .
    Reply With Quote  
     

  5. Thankful user:


  6. #4  
    Registered Member

    Join Date
    Dec 2011
    Posts
    1,952
    Thanks given
    488
    Thanks received
    530
    Rep Power
    364
    Quote Originally Posted by Graham View Post
    free(length) wouldn't make sense because it's not a pointer.

    You should be calling free(package) after every iteration of the loop - were you calling it only once?

    You also change the package pointer during the loop. You need to free() the exact value returned by malloc(), so keep a copy of that around. Calling free() with a pointer to the middle of the memory region that was allocated won't work - it needs to be given a pointer to the start.

    You're also missing an fclose() call at the end.

    btw, there's no need to cast the result of malloc. malloc returns a void pointer, and void pointers can be implicitly cast to any pointer type - there's no need for an explicit cast.
    No, I was doing free(package) in the for loop but the problem is that it is being used afterwards. Also not sure how to free the exact value of malloc(), that's what I was having problems with (Newbie in C if that isn't clear alreadY). Thanks for the help though.
    Need Cheap & HQ Promotional Videos & Trailers?
    Feel free to visit my shop by clicking [Only registered and activated users can see links. ]
    Reply With Quote  
     

  7. #5  
    fumant viriditas quotidiana

    saifix's Avatar
    Join Date
    Feb 2009
    Age
    24
    Posts
    1,241
    Thanks given
    275
    Thanks received
    953
    Rep Power
    3179
    Quote Originally Posted by SilverNova View Post
    No, I was doing free(package) in the for loop but the problem is that it is being used afterwards.
    You can't free it until you're done with it. After printing it to screen with printf() you can free it.

    Quote Originally Posted by SilverNova View Post
    Also not sure how to free the exact value of malloc()
    You pass the returned pointer from `malloc()` to `free()`.

    Code:
    			package += ipSize + tcpSize + etherSize;
    "package" is no longer the pointer returned by malloc. It's the pointer returned by malloc after doing some pointer arithmetic on it. Calling free() on a pointer that wasn't returned by malloc() is undefined behavior (with the exception of NULL, which makes it a no-op).

    So, you can either do "package + ipSize + tcpSize + etherSize" inline in the printf() call or you can create a new pointer to it (char *my_ptr = package + ipSize + tcpSize + etherSize) so you can still do free(package).
    "Im so bluezd out of my box.
    Im so fkd i canr even sens makeas Smoke blunt 420hash e tizday" - A legendary guy (1993 - 2015)
    Quote Originally Posted by nmopal View Post
    I will be creating a grimy dubstep song using these lyrics and vocaloid please prepare your bodies
    Reply With Quote  
     

  8. #6  
    Registered Member

    Join Date
    Dec 2011
    Posts
    1,952
    Thanks given
    488
    Thanks received
    530
    Rep Power
    364
    Quote Originally Posted by saifix View Post
    You can't free it until you're done with it. After printing it to screen with printf() you can free it.



    You pass the returned pointer from `malloc()` to `free()`.

    Code:
    			package += ipSize + tcpSize + etherSize;
    "package" is no longer the pointer returned by malloc. It's the pointer returned by malloc after doing some pointer arithmetic on it. Calling free() on a pointer that wasn't returned by malloc() is undefined behavior (with the exception of NULL, which makes it a no-op).

    So, you can either do "package + ipSize + tcpSize + etherSize" inline in the printf() call or you can create a new pointer to it (char *my_ptr = package + ipSize + tcpSize + etherSize) so you can still do free(package).
    If I do any of the options you mentioned (inline in printf, or new pointer) it just says the free() has reached a breakpoint.
    Need Cheap & HQ Promotional Videos & Trailers?
    Feel free to visit my shop by clicking [Only registered and activated users can see links. ]
    Reply With Quote  
     

  9. #7  
    fumant viriditas quotidiana

    saifix's Avatar
    Join Date
    Feb 2009
    Age
    24
    Posts
    1,241
    Thanks given
    275
    Thanks received
    953
    Rep Power
    3179
    Quote Originally Posted by SilverNova View Post
    If I do any of the options you mentioned (inline in printf, or new pointer) it just says the free() has reached a breakpoint.
    Can you share the code you have after modifying it like that?
    "Im so bluezd out of my box.
    Im so fkd i canr even sens makeas Smoke blunt 420hash e tizday" - A legendary guy (1993 - 2015)
    Quote Originally Posted by nmopal View Post
    I will be creating a grimy dubstep song using these lyrics and vocaloid please prepare your bodies
    Reply With Quote  
     

  10. #8  
    Registered Member

    Join Date
    Dec 2011
    Posts
    1,952
    Thanks given
    488
    Thanks received
    530
    Rep Power
    364
    Quote Originally Posted by saifix View Post
    Can you share the code you have after modifying it like that?
    Edited main post
    Need Cheap & HQ Promotional Videos & Trailers?
    Feel free to visit my shop by clicking [Only registered and activated users can see links. ]
    Reply With Quote  
     

  11. #9  
    Programmer, Veteran and Respected Member



    Join Date
    Mar 2007
    Posts
    5,023
    Thanks given
    2,598
    Thanks received
    3,395
    Rep Power
    5000
    Quote Originally Posted by SilverNova View Post
    Edited main post
    You need to call fclose() after the while loop, not inside it.

    Code:
    
    			char *my_ptr = package + ipSize + tcpSize + etherSize;
    			printf("\nPayload: %s\n\n", package);
    I think you meant to pass my_ptr to printf here btw, not package.
    .
    Reply With Quote  
     

  12. Thankful user:


  13. #10  
    Registered Member

    Join Date
    Dec 2011
    Posts
    1,952
    Thanks given
    488
    Thanks received
    530
    Rep Power
    364
    Quote Originally Posted by Graham View Post
    You need to call fclose() after the while loop, not inside it.

    Code:
    
    			char *my_ptr = package + ipSize + tcpSize + etherSize;
    			printf("\nPayload: %s\n\n", package);
    I think you meant to pass my_ptr to printf here btw, not package.
    Thanks, that was sloppy mistakes on my end. I think that fixed it to be honest. Is there any way to check if there are any leaks? Cause I'm not getting any errors, and all the values seem to be fine when I run it in debug.
    Need Cheap & HQ Promotional Videos & Trailers?
    Feel free to visit my shop by clicking [Only registered and activated users can see links. ]
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [Delta]Memory Leak
    By Neekage in forum Help
    Replies: 8
    Last Post: 07-13-2010, 04:11 PM
  2. Memory Leak
    By Neekage in forum Help
    Replies: 9
    Last Post: 06-11-2010, 08:32 PM
  3. JDBC memory leak?
    By Hidendra_ in forum Help
    Replies: 6
    Last Post: 07-25-2009, 10:11 PM
  4. JDBC memory leak?
    By Hidendra_ in forum RS2 Server
    Replies: 4
    Last Post: 07-23-2009, 08:04 PM
  5. crazy memory leak
    By veer in forum Images
    Replies: 2
    Last Post: 08-23-2008, 11:36 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •