Ethereal-dev: Re: [Ethereal-dev] does the returned string of tvb_format_text gets freed automa

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Charles Levert <chuck@xxxxxxxxxxxxxxxx>
Date: Mon, 15 Nov 2004 10:20:48 -0500
* On Monday 2004-11-15 at 13:20:22 +0100, LEGO wrote:
> proto_tree_add_string(sub_tree, hf_rtsp_X_Vig_Msisdn,tvb,
> value_offset, value_len ,
> tvb_format_text(tvb, value_offset, value_len));
> So, unless  tvb_format_text automatically frees the data once the tvb
> is destoyed this is a leak isn't it?

No.  Read the source.

tvb_format_text() in epan/tvbuff.c calls format_text() in epan/strutil.c
which has a function-static gchar *fmtbuf variable which points to the
same dynamically allocated memory which is returned every single time.

You have to use the content of that memory before it gets reused during
the next call to format_text(), direct or indirect.  If you need to keep
a copy, g_strdup() the result and keep track of that new memory yourself.

Should you free the memory returned by format_text(), fmtbuf will still
point to it and reuse it, and that will most likely result in memory
corruption.