This is a static archive of our old Q&A Site. Please post any new questions and answers at ask.wireshark.org.

memory leak problem with libwireshark!

0

i am working on a project: capture and dissect the network packets using libwireshark when i execute my code with valgrind.. i found this report

7,024 bytes in 143 blocks are definitely lost in loss record 25 of 41

==21679== at 0x4A05809: malloc (vg_replace_malloc.c:149)

==21679== by 0x32D2E33BFA: g_malloc (in /lib64/libglib-2.0.so.0.1200.3)

==21679== by 0x32D2E45A7E: g_strdup (in /lib64/libglib-2.0.so.0.1200.3)

==21679== by 0x5BD2A2E: add_oid (oids.c:145)

==21679== by 0x5BD2BAA: oid_add_from_string (oids.c:178)

==21679== by 0x62935D3: proto_reg_handoff_pkixqualified (packet-pkixqualified-dis-tab.c:7)

==21679== by 0x6339509: register_all_protocol_handoffs (register.c:1593)

==21679== by 0x5BEBFD1: proto_init (proto.c:416)

==21679== by 0x5BCC92F: epan_init (epan.c:104)

==21679== by 0x401FF9: init_libwireshark (trace.c:124)

==21679== by 0x402AAD: main (trace.c:518)

is this the problem with libwireshark only ?? how to get rid of this problem. any help!? thanks!

asked 25 Feb '12, 03:03

Sanny_D's gravatar image

Sanny_D
0182021
accept rate: 50%


One Answer:

1

First: Please note that valgrind may become confused due to various memory optimizations used in libwireshark and Glib.

To use valgrind with Glib based programs please see :

Valgrind and Gnome

Running GLib

(A Google search finds other discussions on this topic).

Also: from the Wireshark sources: epan/emem.c

/*
 * Tools like Valgrind and ElectricFence don't work well with memchunks.
 * Export the following environment variables to make {ep|se}_alloc() allocate each
 * object individually.
 *
 * WIRESHARK_DEBUG_EP_NO_CHUNKS
 * WIRESHARK_DEBUG_SE_NO_CHUNKS

Second: At least some of the cases are because Wireshark does certain memory allocations during initialization which are for the lifetime of the program and which are never freed.

Fixing these hasn't been a priority although I believe there have been efforts from time to time.
Possible cases:

==21679== by 0x6339509: register_all_protocol_handoffs (register.c:1593)
==21679== by 0x5BEBFD1: proto_init (proto.c:416)
==21679== by 0x5BCC92F: epan_init (epan.c:104)
==21679== by 0x401FF9: init_libwireshark (trace.c:124)

To get "clean" valgind output, the simplest solution may be to configure valgrind to ignore certain of these memory leaks.

Third: Other cases may very well be memory leaks which are more "real": e.g., memory allocated during dissection of a frame which is not freed properly when the dissection is complete.

We gladly accept patches to fix specific instances of memory leaks. :) :)

answered 25 Feb '12, 06:17

Bill%20Meier's gravatar image

Bill Meier ♦♦
3.2k1850
accept rate: 17%

edited 27 Feb '12, 06:21

And it's not clear it's worth fixing the cases where Wireshark does certain memory allocations during initialization which are for the lifetime of the program and which are never freed - it's faster to free that memory by simply terminating the process with exit() or a return from main() than to manually free it before exiting.

(27 Feb '12, 12:23) Guy Harris ♦♦

yes. but i want to run the application for a long time on server side, so freeing with exit() or return from main() wont serve the purpose.

is any other way to remove this unwanted leaks, like cleanup routines.

thanks!

(19 Jun '12, 00:34) Sanny_D

There's no way to remove memory allocations that valgrind misidentifies as leaks; there might, as per the links Bill suggests, be ways to get valgrind to stop falsely claiming that they're leaks, so that it identifies only real leaks. From looking at add_oid(), what valgrind is reporting there is unlikely to be a leak unless add_oid() isn't properly updating the tree and therefore losing the stuff it allocates.

And, no, there's no way to remove allocations of stuff that's intended to be allocated during the entire Wireshark run.

(19 Jun '12, 01:05) Guy Harris ♦♦
6,552 bytes in 273 blocks are definitely lost in loss record 49,162 of 49,380
==25873==    at 0x4A05809: malloc (vg_replace_malloc.c:149)
==25873==    by 0x32D2E33BFA: g_malloc (in /lib64/libglib-2.0.so.0.1200.3)
==25873==    by 0x32D2E10AF1: g_array_sized_new (in /lib64/libglib-2.0.so.0.1200.3)
==25873==    by 0x5D97344: proto_register_diameter (packet-diameter.c:1482)
==25873==    by 0x634036E: register_all_protocols (register.c:234)
==25873==    by 0x5BECF25: proto_init (proto.c:395)
==25873==    by 0x5BCD92F: epan_init (epan.c:104)
==25873==    by 0x392DCB48: init_libwireshark (trace_23rdfb.c:36)
==25873==    by 0x392DDA46: init_trace (trace_23rdfb.c:582)

this is the report after turning off the glib optimization.. now what could be wrong?

(19 Jun '12, 01:18) Sanny_D

What's wrong is that

1) you have Diameter dictionaries

and

2) either turning off the glib optimization isn't sufficient to keep valgrind from being confused or somehow the array isn't getting freed (it's probably the build_dict.hf array if this is a recent version of Wireshark, and that array is freed - but its content isn't freed because it's been registered as a set of fields - so the answer is probably "turning off the glib optimization isn't sufficient to keep valgrind from being confused").

(19 Jun '12, 01:48) Guy Harris ♦♦

yes. but i want to run the application for a long time on server side ...

Note well: the code in various dissectors (in libwireshark) accumulates state (using memory) as frames are dissected.

So, expecting to "run the application for a long time on server side" without memory usage increasing (which seems the implication of your questions) is a non-starter.

Am I missing something (or incorrect about how you are using libwireshark ?)

(19 Jun '12, 07:12) Bill Meier ♦♦
showing 5 of 6 show 1 more comments