Wireshark-dev: Re: [Wireshark-dev] I need wmem_alloc advice please
From: Evan Huus <[email protected]>
Date: Thu, 15 Sep 2016 12:57:02 -0400
Based on the name, this looks like memory that is only ever needed in the scope of dissection for a single packet. If that is the case, it should allocated in packet-scope when needed and not be global at all.

Evan

On Thu, Sep 15, 2016 at 12:48 PM, Pascal Quantin <[email protected]> wrote:
Hi Paul,

2016-09-15 18:44 GMT+02:00 Paul Offord <[email protected]>:

My porting of TRANSUM from LUA to C continues.  I now have a working dissector but I then read README.developer and noted the guidance regarding use of static buffers.  I assume this applies to arrays too and so I’m now trying to convert the statically defined arrays to versions based on dynamic memory allocation.  I’ve converted some string buffers and a simple gboolean array without problems.  I then tried to convert an array of structures and this caused crashes all over the place.

 

Before the conversion I had this as a global variable:

 

    PKT_INFO sub_packet[MAX_SUBPKTS_PER_PACKET];

 

I replaced this with a global variable:

 

    PKT_INFO *sub_packet;

 

And this in my init function:

 

    sub_packet = (PKT_INFO *)wmem_alloc(wmem_file_scope(), (MAX_SUBPKTS_PER_PACKET * sizeof(PKT_INFO)));

 

I subsequently use it like this:

 

    sub_packet[i].frame_number = 0;

 

I seem to be getting memory corruption as a result of this change.  Weird things happen, like I lose preference variables.

 

What am I doing wrong here?


File scope memory is automatically freed each time a preference is changed, or a file is reloaded. If you need to keep some things persistent during all Wireshark instance, consider using epan scope memory for those variables instead.

Regards,
Pascal.

___________________________________________________________________________
Sent via:    Wireshark-dev mailing list <[email protected]>
Archives:    https://www.wireshark.org/lists/wireshark-dev
Unsubscribe: https://www.wireshark.org/mailman/options/wireshark-dev
             mailto:[email protected]wireshark.org?subject=unsubscribe