-
February 20th, 2009, 09:30 AM
#1
[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!
-
February 20th, 2009, 09:52 AM
#2
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
-
February 20th, 2009, 10:29 AM
#3
Re: memory leak in my memset usage?
4. How do you check for memory leaks?
-
February 20th, 2009, 10:43 AM
#4
Re: memory leak in my memset usage?
Originally Posted by lab1
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.
-
February 20th, 2009, 11:27 AM
#5
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.
-
February 20th, 2009, 11:55 AM
#6
Re: memory leak in my memset usage?
Originally Posted by lab1
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.
-
February 20th, 2009, 01:16 PM
#7
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.
-
February 20th, 2009, 01:40 PM
#8
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.
-
February 20th, 2009, 01:57 PM
#9
Re: memory leak in my memset usage?
Originally Posted by lab1
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?
-
February 20th, 2009, 02:24 PM
#10
Re: memory leak in my memset usage?
Last edited by lab1; February 23rd, 2009 at 07:23 AM.
-
February 20th, 2009, 02:31 PM
#11
Re: memory leak in my memset usage?
Originally Posted by lab1
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.
-
February 20th, 2009, 02:37 PM
#12
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.
-
February 20th, 2009, 02:41 PM
#13
Re: memory leak in my memset usage?
In that case report the problem to the implementer.
-
February 20th, 2009, 02:55 PM
#14
Re: memory leak in my memset usage?
Originally Posted by lab1
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?
-
February 20th, 2009, 02:57 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|