CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    [RESOLVED] memory leak in my memset usage?

    Hello,

    I have the following code that seems to generate a memory leak:

    struct sockaddr_in serverAddr;
    memset((char*)&serverAddr, 0, sizeof(_serverAddr));

    I used a profiler to track the leak down to the memset call. It is only 1 block of memory, but I am wondering if I am using memset wrong?

    Can anyone analyze this?

    Thanks!

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: memory leak in my memset usage?

    1. Why are you casting &serverAddr to char*?
    2. What is _serverAddr?
    3. Define "seems to generate a memory leak".
    Victor Nijegorodov

  3. #3
    Join Date
    May 2007
    Posts
    811

    Re: memory leak in my memset usage?

    4. How do you check for memory leaks?

  4. #4
    Join Date
    Nov 2003
    Posts
    1,405

    Re: memory leak in my memset usage?

    Quote Originally Posted by lab1 View Post
    Can anyone analyze this?
    It's bad coding. You shouldn't "null" a struct like that. Use a constructor instead, or an initialization method.

    Not that I think you actually have a memory leak. It's probably just that the struct occupies more memory on the stack than you thought it would, but that's not a memory leak. I could be a result of data alignments performed by the compiler. For memory to be leaked it has to be allocated on the heap, otherwise leaking is imposssible.
    Last edited by _uj; February 20th, 2009 at 11:26 AM.

  5. #5
    Join Date
    Apr 2001
    Posts
    1,029

    Re: memory leak in my memset usage?

    this is standard code from tcp/ip examples. If it is bad code then it is in the Linux kernel as well as examples in all the major tcp/ip books.

    _serverAddr is just serverAddr. The underscore was a typo in my paste.

    I used glowcode 6.2 to find the memory leak.

    I am casting &serverAddr to char* because thats how all the examples do it. The leak is there regardless if you cast it or not.

    I am trying to create a very small sample program that will replicate the leak. i will post it here if possible.
    Last edited by lab1; February 20th, 2009 at 11:35 AM.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,405

    Re: memory leak in my memset usage?

    Quote Originally Posted by lab1 View Post
    this is standard code from tcp/ip examples. If it is bad code then it is in the Linux kernel as well as examples in all the major tcp/ip books.
    Well, it's bad C++ code but I guess it's passable C code (The Linux kernel is writen in C).

    Still, "nulling" a stack-allocated C struct cannot leak anything unless the struct holds some pointer to heap-allocated data.

    What you consider a leak is caused by compiler induced allignement of the struct on the stack. It's just a question of "fitting". The "extra" memory used will be returned when the function returns.
    Last edited by _uj; February 20th, 2009 at 12:03 PM.

  7. #7
    Join Date
    Apr 2001
    Posts
    1,029

    Re: memory leak in my memset usage?

    ok memset was not the issue.. here is the most compact program I can generate that creates the leak:



    Any idea why this code causes 1 block of memory to leak? Thanks!
    Last edited by lab1; February 23rd, 2009 at 07:24 AM.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: memory leak in my memset usage?

    There is no memory leak in that code.

    I suspect you're trying to micro-manage memory usage----don't. That isn't your responsibility, it's that of the OS. The only memory you're responsible for managing is that which your code dynamically allocates. Since you don't do any dynamic allocation, you have no control over how much memory is used by the program.

  9. #9
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: memory leak in my memset usage?

    Quote Originally Posted by lab1 View Post
    Any idea why this code causes 1 block of memory to leak?
    Does the leak go away if you remove that loop and call WSACleanup?

  10. #10
    Join Date
    Apr 2001
    Posts
    1,029

    Re: memory leak in my memset usage?

    gg
    Last edited by lab1; February 23rd, 2009 at 07:23 AM.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: memory leak in my memset usage?

    Quote Originally Posted by lab1 View Post
    Even the code below have the exact same leak:
    You keep using the word. I do not think it means what you think it means.

  12. #12
    Join Date
    Apr 2001
    Posts
    1,029

    Re: memory leak in my memset usage?

    Use my identical code and run it in a profiler. the socket() call contains a memory leak. You shouldnt say there is no leak unless you verify it. There is indeed a memory leak in the winsock socket() call.

    A quick google will show you many other people have found this problem, but there is no answer yet.

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: memory leak in my memset usage?

    In that case report the problem to the implementer.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: memory leak in my memset usage?

    Quote Originally Posted by lab1 View Post
    Use my identical code and run it in a profiler. the socket() call contains a memory leak. You shouldnt say there is no leak unless you verify it. There is indeed a memory leak in the winsock socket() call.

    A quick google will show you many other people have found this problem, but there is no answer yet.
    What is this profiler you're referring to and what exactly is it saying?

  15. #15
    Join Date
    Apr 2001
    Posts
    1,029

    Re: memory leak in my memset usage?

    I am using the lastest version of GlowCode 6.2. It says that there is a block of memory leaked when running my code as you see above. The leak is attributed to the socket() call.

Page 1 of 2 12 LastLast

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