Re: Memory leak when internal pointer passed out as parameter
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 03 Apr 2007 13:03:24 -0700
"Mike" <mail2mz@xxxxxxxxx> writes:
On Apr 3, 2:29 pm, rober...@xxxxxxxxxxxxxxxxxx (Walter Roberson)
wrote:
In article <1175623345.081280.166...@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
Mike <mail...@xxxxxxxxx> wrote:
I have following existing code. And there is memory leak. Anyone know
how to get ride of it?
Are you certain that it is a memory leak, and not a memory
fragmentation problem?
Rational Purify checked the code, and reported memory leak on foo1
when we allocate memory. I assum e that the compiler will allocate a
new block of memory when foo1 returns. Then the memory allocated
within foo1 will remain in the system heap forever. However I have no
way to verify it.
(Please don't quote signatures.)
Her's the code you posted:
my_struc * foo1( )
{
my_struc * tmp;
tmp = (my_struc *)calloc(1, sizeof(my_struc));
return tmp;
}
void main()
{
my_struc *mainPtr;
mainPtr = foo1();
free(mainPtr);
}
There is no memory leak in that code, though there are other problems:
main returns int; change "void main()" to "int main(void)".
Add a "return 0;" at the end of your main function.
foo1 takes no arguments. You should say so explicitly:
"my_struc *foo1(void)".
Don't cast the result of malloc() or calloc().
It's very likely that malloc() is better than calloc(); calloc()
initializes the allocated memory to all-bits-zero, which is rarely
useful.
Read the FAQ, <http://www.c-faq.com>.
In general, there should be a call to free() for every call to
malloc() or calloc() (realloc() complicates things a bit, but you're
not using that). In the code you showed us, you properly free the
allocated memory. In the code you didn't show us, there must be a
case where you don't free the allocated memory. That's all we can
tell from what you've posted. Rational Purify found the memory leak
for you; can it be persuaded to tell you more about where it happens,
for example, where foo1 was called from?
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.
- Follow-Ups:
- Re: Memory leak when internal pointer passed out as parameter
- From: Mr John FO Evans
- Re: Memory leak when internal pointer passed out as parameter
- References:
- Memory leak when internal pointer passed out as parameter
- From: Mike
- Re: Memory leak when internal pointer passed out as parameter
- From: Walter Roberson
- Re: Memory leak when internal pointer passed out as parameter
- From: Mike
- Memory leak when internal pointer passed out as parameter
- Prev by Date: Re: portable bit ops
- Next by Date: Re: Memory leak
- Previous by thread: Re: Memory leak when internal pointer passed out as parameter
- Next by thread: Re: Memory leak when internal pointer passed out as parameter
- Index(es):
Relevant Pages
|