Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-07-2010, 06:58 PM
|
#1
|
LQ Newbie
Registered: Jun 2010
Posts: 19
Rep:
|
Memory leak in C - A few questions
I'm doing some raw socket scripting in C. Everything works fine.
However, I monitored the memory using top, and it shows that the memory used by my script is increasing over time.
I don't have that many mallocs (maybe a couple), but lots of pointers. Even though I free those pointers (used as local variables in functions), the memory used by my program is still consuming memory (although slower than if free() is not being used).
I would like to know what situations may produce memory leak and how to deal with them. Do the char[] variables need to be freed?
I'm just asking for some guidelines.
Thank you
Last edited by aztroboy; 12-07-2010 at 07:00 PM.
|
|
|
12-07-2010, 11:38 PM
|
#2
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
In general, every piece of memory that is malloced should eventually be freed. But you should not free memory that was not allocated from the heap.
However, you can also have 'leaks' if you do not free system resources (for example, you may be failing to close open sockets that are no longer being used).
Valgrind can be useful for finding leaks if you get really stuck.
|
|
|
12-07-2010, 11:40 PM
|
#3
|
Senior Member
Registered: Dec 2008
Posts: 4,732
|
Quote:
But you should not free memory that was not allocated from the heap.
|
Shouldn't the sentence be:
But you cannot free memory that was not allocated from the heap.
Correct me if I am wrong.
|
|
|
12-07-2010, 11:52 PM
|
#4
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Quote:
Originally Posted by anishakaul
Shouldn't the sentence be:
But you cannot free memory that was not allocated from the heap.
|
You are correct in the sense that in an environment like glibc, calling free on a pointer that is outside the heap will generate an error. However, the standard simply says that the behaviour is not defined, and in some environments freeing such a chunk of memory not created by alloc or calloc will appear to work (but corrupt the memory around the chunk).
Last edited by neonsignal; 12-07-2010 at 11:59 PM.
|
|
1 members found this post helpful.
|
12-08-2010, 12:02 AM
|
#5
|
Senior Member
Registered: Dec 2008
Posts: 4,732
|
Thanks, that was informative but when you say:
Quote:
in some environments freeing such a chunk of memory not on the heap will appear to work (but corrupt the memory around the chunk).
|
Are you talking about static arrays ? Dynamic memory has to be from heap, isn't it ? Or I am barking at the wrong tree ?
|
|
|
12-08-2010, 01:22 AM
|
#6
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Quote:
Originally Posted by anishakaul
Are you talking about static arrays ? Dynamic memory has to be from heap, isn't it ?
|
Yes, I was thinking about arrays declared statically or on the stack.
|
|
1 members found this post helpful.
|
12-08-2010, 04:01 AM
|
#8
|
Member
Registered: Jun 2010
Distribution: Ubuntu 10.04, Cent OS 5.5, CLE3
Posts: 51
Rep:
|
Quote:
Originally Posted by aztroboy
However, I monitored the memory using top, and it shows that the memory used by my script is increasing over time.
|
Which field are you looking in 'top' command ? It (top) does not accurately represent malloc/free. Neither do most monitoring tools like top. It's quite possible that freeing memory may not be reflected in top.
Only tools that directly interact with application can give an accurate view. As neonsignal mentioned, you can use Valgrind, which would be my first choice (I'm a lazy-to-type GUI lover :P), or you could use mtrace (simple, yet effective) to find the source of leaks.
Last edited by wagaboy; 12-08-2010 at 04:03 AM.
|
|
1 members found this post helpful.
|
12-08-2010, 06:16 AM
|
#9
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811
Rep: 
|
In the name of the Flying Spaghetti Monster,
Quote:
Originally Posted by anishakaul
how to manually allocate memory from stack is still a question for me
|
Use alloca(), but: - Returning from the current function is the only way you can free the memory; you can't use free() on memory allocated with alloca().
- It's not a good idea to use alloca() for large amounts of memory.
- This function is not as portable as one might like.
- There are other arcane points of caution. Read about them on the man page.
|
|
2 members found this post helpful.
|
12-08-2010, 08:38 AM
|
#10
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
There is a smallish class of functions which silently allocate memory, and for which the expectation is that the caller will be responsible to free said memory. The function strdup() is an example of one such function. I'm not sure if there is a way to get a concise list of functions that behave similarly. If in doubt, check the repective man pages for functions you use.
--- rod.
|
|
1 members found this post helpful.
|
12-08-2010, 07:34 PM
|
#11
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
Quote:
Originally Posted by theNbomr
There is a smallish class of functions which silently allocate memory, and for which the expectation is that the caller will be responsible to free said memory.
|
Good point; the following are some of the functions that can allocate from the heap:
calloc, malloc
posix_memalign, memalign, valloc
strdup, strndup
wcsdup
asprintf, vasprintf
tempnam
getcwd, get_current_dir_name
realpath
open_memstream
mhash_get_hash_name, mhash_get_keygen_name
pth_sfiodisc
There are others that use malloc and free internally, but the application does not have to explicitly free the memory.
Last edited by neonsignal; 12-08-2010 at 07:59 PM.
|
|
1 members found this post helpful.
|
12-08-2010, 09:07 PM
|
#12
|
Senior Member
Registered: Dec 2008
Posts: 4,732
|
wje_lq,
That was helpful, Thanks for the enlightenment!
Quote:
Originally Posted by wje_lq
Returning from the current function is the only way you can free the memory
|
Well, does this mean the actual return statement will have to be used or the memory will be automatically freed when the scope of the function ends ?
I read this doc: http://www.gnu.org/s/libc/manual/htm...ages-of-Alloca
Spoilsport:
Quote:
The alloca() function is machine and compiler dependent. On many systems its implementation is buggy. Its use is discouraged.
On many systems alloca() cannot be used inside the list of arguments of a function call, because the stack space reserved by alloca() would appear on the stack in the middle of the space for the function arguments.
|
Last edited by Aquarius_Girl; 12-08-2010 at 09:09 PM.
|
|
|
12-08-2010, 09:43 PM
|
#13
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
Memory leaks are very hard to trouble shoot. (especially without code  )
|
|
0 members found this post helpful.
|
12-08-2010, 09:44 PM
|
#14
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811
Rep: 
|
In the name of the Flying Spaghetti Monster,
No, you don't need the actual return statement for the allocaton to be released.
I'd be curious to see documentation on how it's buggy. I'd risk it in Linux. I have no data to support thinking this way; it's just worth trying.
If you're running a recent version of Linux, it looks as though the scariness in the link I posted as the man page might be too hasty. Here's the scoop on the man page as distributed with Debian lenny. If nothing on that man page gives you specific pause, I'd say use it if you want.
Code:
NOTES
The alloca() function is machine- and compiler-dependent. For certain
applications, its use can improve efficiency compared to the use of
malloc(3) plus free(3). In certain cases, it can also simplify memory
deallocation in applications that use longjmp(3) or siglongjmp(3).
Otherwise, its use is discouraged.
Because the space allocated by alloca() is allocated within the stack
frame, that space is automatically freed if the function return is
jumped over by a call to longjmp(3) or siglongjmp(3).
Do not attempt to free(3) space allocated by alloca()!
Notes on the GNU Version
Normally, gcc(1) translates calls to alloca() with inlined code. This
is not done when either the -ansi, -std=c89, -std=c99, or the
-fno-builtin option is given (and the header <alloca.h> is not
included). But beware! By default the glibc version of <stdlib.h>
includes <alloca.h> and that contains the line:
#define alloca(size) __builtin_alloca (size)
with messy consequences if one has a private version of this function.
The fact that the code is inlined means that it is impossible to take
the address of this function, or to change its behavior by linking with
a different library.
The inlined code often consists of a single instruction adjusting the
stack pointer, and does not check for stack overflow. Thus, there is
no NULL error return.
BUGS
There is no error indication if the stack frame cannot be extended.
(However, after a failed allocation, the program is likely to receive a
SIGSEGV signal if it attempts to access the unallocated space.)
On many systems alloca() cannot be used inside the list of arguments of
a function call, because the stack space reserved by alloca() would
appear on the stack in the middle of the space for the function argu-
ments.
|
|
|
All times are GMT -5. The time now is 03:37 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|