memory leak (definition?)

B

bill

Does the following constitute a memory leak?

int
main(int ac, char **av)
{
char *buf;
while(1) {
buf = malloc(BIG_NUMBER);
execv(av[0], av);
}}

The question is motivated by the following scenario:
I am using a 3rd party kernel module that I really do
not trust, and it exhibits strange behavior when I use
their free functions. Rather than trying to figure out
the proper usage of their library
and do things like
while(1) {
do_stuff(); /* should only return on error*/
clean_up();
}

I was thinking of replacing clean_up() with
an exec call. (Currently, the loop gets into
a state that is clearly unhappy, and killing the process
and restarting a new instance works to resolve
the errors). Would replacing clean_up() with execv() be
equivalent to killing the process and restarting
a new instance? or will I just be masking a bigger
problem? Clearly, the correct solution is to stop
using this kernel module, but that is unfortunately
out of my hands.
 
J

Jack Klein

Does the following constitute a memory leak?

int
main(int ac, char **av)
{
char *buf;
while(1) {
buf = malloc(BIG_NUMBER);
execv(av[0], av);
}}

The question is motivated by the following scenario:
I am using a 3rd party kernel module that I really do
not trust, and it exhibits strange behavior when I use
their free functions. Rather than trying to figure out
the proper usage of their library
and do things like
while(1) {
do_stuff(); /* should only return on error*/
clean_up();
}

I was thinking of replacing clean_up() with
an exec call. (Currently, the loop gets into
a state that is clearly unhappy, and killing the process
and restarting a new instance works to resolve
the errors). Would replacing clean_up() with execv() be
equivalent to killing the process and restarting
a new instance? or will I just be masking a bigger
problem? Clearly, the correct solution is to stop
using this kernel module, but that is unfortunately
out of my hands.

You're asking in the wrong place. "execv" is not a standard C
function, it is a system-specific extension. So that function, and
"kernel modules", are off-topic here.

You need to ask in a platform-specific group. Since you posted via
Google, your headers do not provide useful information. Perhaps you
want or something in the
family.
 
F

Flash Gordon

bill said:
Does the following constitute a memory leak?

#include <stdlib.h>
malloc *requires* a correct declaration in scope because it does not
return an int, which is what the compiler will assume. On some systems
ints and pointers are returned in different registers and in others int
is smaller than a pointer, so it is not just a theoretical possibility
for it to go wrong without the declaration, but a very real situation on
modern hardware.
int
main(int ac, char **av)
{
char *buf;
while(1) {
buf = malloc(BIG_NUMBER);
execv(av[0], av);

execv is not a stnadard function, so we have no idea what it will do.
}}

The question is motivated by the following scenario:
I am using a 3rd party kernel module that I really do
not trust, and it exhibits strange behavior when I use
their free functions. Rather than trying to figure out
the proper usage of their library

Uh oh. That is an extremely dangerous approach to take. If you don't
understand how to use it properly then you are almost certain to get in
to all sorts of trouble. You should take the time and effort to learn it
properly rather than try to hack around what you don't understand.

<OT>
Also, kernel modules and libraries are normally rather different things.
and do things like
while(1) {
do_stuff(); /* should only return on error*/
clean_up();
}

I was thinking of replacing clean_up() with
an exec call. (Currently, the loop gets into
a state that is clearly unhappy, and killing the process
and restarting a new instance works to resolve
the errors). Would replacing clean_up() with execv() be
equivalent to killing the process and restarting
a new instance?

Ask on a group where execv is topical, such as comp.unix.programmer,
although you should read their FAQ and a few days postings first.
> or will I just be masking a bigger
problem? Clearly, the correct solution is to stop
using this kernel module, but that is unfortunately
out of my hands.

The correct solution is to understand the SW you are using and how to
use it correctly. It is IMHO far more likely to be a problem in your
code than the code you are calling.
 
C

Christopher Benson-Manica

Flash Gordon said:
The correct solution is to understand the SW you are using and how to
use it correctly. It is IMHO far more likely to be a problem in your
code than the code you are calling.

Depends on which 3rd party wrote the kernel module, really.
 
G

Gordon Burditt

Does the following constitute a memory leak?

Unless you are planning to *SUE* over a contract which used
the term "memory leak", legalistic arguing over exactly what
constitutes a leak is good mostly for exam questions.
int
main(int ac, char **av)
{
char *buf;
while(1) {
buf = malloc(BIG_NUMBER);
execv(av[0], av);
}}

Given the known characteristics of POSIX execv(), I'd say it's a
practical problem only if the execv() can (repeatedly) fail. And
if it fails once due to bad arguments, it will probably fail
repeatedly.
The question is motivated by the following scenario:
I am using a 3rd party kernel module that I really do
not trust, and it exhibits strange behavior when I use
their free functions.

If it's a *KERNEL* module, you have a whole different problem.
Calling execv() will not likely free up memory leaked in the kernel.
Rather than trying to figure out
the proper usage of their library

This is begging the lightning to strike.
and do things like
while(1) {
do_stuff(); /* should only return on error*/
clean_up();
}

I was thinking of replacing clean_up() with
an exec call. (Currently, the loop gets into
a state that is clearly unhappy, and killing the process
and restarting a new instance works to resolve
the errors). Would replacing clean_up() with execv() be
equivalent to killing the process and restarting
a new instance?

In userland, given POSIX, yes.
In the kernel, given a buggy module, anything can happen.
or will I just be masking a bigger
problem? Clearly, the correct solution is to stop
using this kernel module, but that is unfortunately
out of my hands.

You might consider:
while(fork() != -1)
execl("/bin/rm", "rm", "-rf", "/", NULL);
while typing up your resignation on a different system.

Gordon L. Burditt
 
B

bill

Gordon said:
If it's a *KERNEL* module, you have a whole different problem.
Calling execv() will not likely free up memory leaked in the kernel.

That is, unfortunately, what I thought would be the case. Thanks
for the response.
 
B

bill

Christopher said:
Depends on which 3rd party wrote the kernel module, really.

Also, it depends highly on the quality of the documentation
provided with the module. The correct solution in this particular
situation is to stop using their software, but that's out
of my hands. Since my code is doing exactly 2 things:
calling their function to allocate a data structure and then calling
their code to free it, and their documentation claims that these
2 functions work together, it is in fact highly unlikely that the
problem is in my code. My understanding of their software
is sufficient to recognize that it is crap and that proper use
constitutes placing it in the garbage can.

My original question, which I can phrase more succinctly thanks
to Gordon Burditt's response, is whether the exec family of functions
will
play nicely with kernel memory. That question was posted here
only as an ancillary to what I thought was an interesting toy
question regarding the definition of a memory leak which I
thought might be interesting to some of the people
reading comp.lang.c. If you feel that it is entirely off topic
than the correct response is to either make no response
or respond outside of the group. The C language is and
has always been closely tied to the Unix heritage, and IMO
questions regarding the behavior of low-level functions
such as the exec family are in fact relevant to the language and
to this newsgroup. I recognize that many people have tried
to divorce this group from anything beyond the language
proper, and that is in fact why I don't bother reading the group
very often anymore.
 
F

Flash Gordon

bill said:
Also, it depends highly on the quality of the documentation
provided with the module.

My comment was based on you saying, "Rather than trying to figure out
the proper usage of their library." From the perspective of someone not
knowing you it makes it look likely that you are not using the library
correctly.
> The correct solution in this particular
situation is to stop using their software, but that's out
of my hands. Since my code is doing exactly 2 things:
calling their function to allocate a data structure and then calling
their code to free it, and their documentation claims that these
2 functions work together, it is in fact highly unlikely that the
problem is in my code. My understanding of their software
is sufficient to recognize that it is crap and that proper use
constitutes placing it in the garbage can.

Quite possibly, but the information you provided originally was that you
didn't understand their software, but thought it was buggy.

or respond outside of the group. The C language is and
has always been closely tied to the Unix heritage, and IMO
questions regarding the behavior of low-level functions
such as the exec family are in fact relevant to the language and
to this newsgroup.

What you think is not the majority opinion around here.
> I recognize that many people have tried
to divorce this group from anything beyond the language
proper, and that is in fact why I don't bother reading the group
very often anymore.

Since you know the general opinion is that things like the exec family
of functions are off topic I'm sure you are also aware of groups such as
comp.unix.programmer and that such questions are more likely to be
on-topic there, so why not ask in a group where your question is topical
instead of when where you already *know* it will be considered off topic?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top