Re: Memory leak when internal pointer passed out as parameter



"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"
.



Relevant Pages

  • Re: Update #1 and memory leaks!
    ... traditional memory leaks dont occur anymore... ... The garbage collector _do_ know about all the allocated memory, ... > I thought we settled on it being a memory leak. ...
    (borland.public.delphi.non-technical)
  • [NT] Services for UNIX 2.0 Suffer from a Remotely Triggered Memory Leak
    ... Services for UNIX 2.0 Suffer from a Remotely Triggered Memory Leak ... 2000 are not affected by the vulnerability. ... * A vulnerability that could enable an attacker to cause the NFS service ...
    (Securiteam)
  • RE: Memory increasing even when in debug-break
    ... there are 2 types of memory leak in .Net: ... More interesting is the debugger breaks while memory still increasing ... Microsoft Online Community Support ...
    (microsoft.public.vsnet.debugging)
  • Re: Memory leak in the Driver
    ... will it also work for device driver ??? ... if the following application is used then, there is no memory leak. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Memory Leak
    ... Regarding .Net memory leak issue, an important step is determing if it is ... should not use the "Mem Usage" column in task manager which is misleading. ... Microsoft Online Community Support ...
    (microsoft.public.dotnet.languages.csharp)