[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!
Re: memory leak in my memset usage?
1. Why are you casting &serverAddr to char*? :confused:
2. What is _serverAddr? :confused::confused:
3. Define "seems to generate a memory leak". :rolleyes:
Re: memory leak in my memset usage?
4. How do you check for memory leaks?
Re: memory leak in my memset usage?
Quote:
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.
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.
Re: memory leak in my memset usage?
Quote:
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.
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!
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.
Re: memory leak in my memset usage?
Quote:
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?
Re: memory leak in my memset usage?
Re: memory leak in my memset usage?
Quote:
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.
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.
Re: memory leak in my memset usage?
In that case report the problem to the implementer.
Re: memory leak in my memset usage?
Quote:
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?
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.