--- main.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/main.c b/main.c index 86acfb7..58622f8 100644 --- a/main.c +++ b/main.c @@ -142,6 +142,7 @@ static struct globals *alfred_init(int argc, char *argv[]) int main(int argc, char *argv[]) { struct globals *globals; + int ret;
globals = alfred_init(argc, argv);
@@ -150,15 +151,21 @@ int main(int argc, char *argv[])
switch (globals->clientmode) { case CLIENT_NONE: - return alfred_server(globals); + ret = alfred_server(globals); break; case CLIENT_REQUEST_DATA: - return alfred_client_request_data(globals); + ret = alfred_client_request_data(globals); break; case CLIENT_SET_DATA: - return alfred_client_set_data(globals); + ret = alfred_client_set_data(globals); break; + default: + ret = 0; }
- return 0; + free(globals->interface); + free(globals->mesh_iface); + free(globals); + + return ret; }
On Mon, Sep 09, 2013 at 03:39:02AM +0200, Nils Schneider wrote:
main.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/main.c b/main.c index 86acfb7..58622f8 100644 --- a/main.c +++ b/main.c @@ -142,6 +142,7 @@ static struct globals *alfred_init(int argc, char *argv[]) int main(int argc, char *argv[]) { struct globals *globals;
int ret;
globals = alfred_init(argc, argv);
@@ -150,15 +151,21 @@ int main(int argc, char *argv[])
switch (globals->clientmode) { case CLIENT_NONE:
return alfred_server(globals);
break; case CLIENT_REQUEST_DATA:ret = alfred_server(globals);
return alfred_client_request_data(globals);
break; case CLIENT_SET_DATA:ret = alfred_client_request_data(globals);
return alfred_client_set_data(globals);
break;ret = alfred_client_set_data(globals);
- default:
}ret = 0;
- return 0;
- free(globals->interface);
- free(globals->mesh_iface);
- free(globals);
- return ret;
Hrm, apart from that this patch lacks a commit message and signoff, what does it help freeing stuff just before the program exits? This will free anything automatically anyway ...
Cheers, Simon
On Mon, Sep 09, 2013 at 05:08:31PM +0200, Simon Wunderlich wrote:
Hrm, apart from that this patch lacks a commit message and signoff, what does it help freeing stuff just before the program exits? This will free anything automatically anyway ...
Hm, the advantage of freeing such memory in the code is that for one thing it makes valgrind happy :P. And this in turn makes finding "real" memory leaks easier. And besides, isn't it a better coding style to free such global variables, too?
I think I'd love seeing a PATCHv2 of this one. And I guess only a short, two sentence commit message should be sufficient as this isn't a very invasive patch. For instance:
"This patch fixes a non-serious memory leak on program exit by freeing the memory allocated for the globals variables. This makes valgrind happy, too.
Signed-off-by: ..."
Cheers, Linus
PS: Sorry, I had a glance over this patch before it got submitted but even though I should be somehow familiar with the prefered coding style, I didn't object.
b.a.t.m.a.n@lists.open-mesh.org