[Winpcap-users] Problem Freeing Array Returned bypcap_list_datalinks

Gianluca Varenni gianluca.varenni at cacetech.com
Mon Sep 11 16:26:38 GMT 2006


----- Original Message ----- 
From: "Yaser Zhian" <yzt at gmx.net>
To: <winpcap-users at winpcap.org>
Sent: Sunday, September 10, 2006 1:08 AM
Subject: Re: [Winpcap-users] Problem Freeing Array Returned 
bypcap_list_datalinks


> Guy Harris wrote:
>> Yaser Zhian wrote:
>>
>>> The following code also generates the same failure:
>>>
>>>  int * dltlist = 0;
>>>  int r = pcap_list_datalinks (m_capturehnd, &dltlist);
>>>  free (dltlist);
>>>
>>> The problem occurs in the free() call. Am I doing something wrong?
>>
>> No.
>>
>> That code should work.  If it doesn't work, it's either
>>
>>     1) a bug in WinPcap;
>>
>>     2) a bug in the C runtime;
>>
>>     3) possibly a result of WinPcap being built for a different version
>> of the C runtime than your application (so that the version of malloc()
>> called by pcap_list_datalinks() doesn't match the version of free()
>> called by your application), but I'm not an expert in the MSVC++ C
>> runtime so I don't know whether that could or would be the case (UN*X C
>> runtimes tend not to come in as many flavors and tend not to have that
>> particular problem).
>>
>
> Thanks for the quick reply.
>
> I guess the 3rd scenario is more likely than the first two. And I
> guess I'd have to build WinPcap from source, don't I?

I can confirm that the problem is exactly 3). And recompiling WinPcap 
doesn't help a lot, in the sense that in any case you'd need to recompile a 
different version of wpcap.dll for *every* C-Runtime you are using (and this 
means debug and release, single threaded, multithreaded, multithreaded DLL, 
bla bla bla).

This is a known problem with malloc/free's and the microsoft libraries: you 
should use malloc and free coming from the same C-runtime. Basically the 
rule is to allocate and free memory in the same module (being it a DLL, 
executable or static library). Never expose an API returning some chunk of 
memory that should be later freed by some "unknown" free routine (free() is 
the C runtime one, there are a lot of other ones!!). Same for file 
descriptors (FILE*). That's one of the reasons why for example in 
libpcap/WinPcap you have pcap_findalldevs and pcap_freealldevs().

We can discuss for hours on whether this limitation in the C-runtime makes 
sense or is brain damaged, IMHO it makes a lot of sense, and I consider a 
really bad practice to have a function  (apart malloc()) that returns a 
chunk of memory that should be freed by the caller with free().


Have a nice day
GV


>
> (Interestingly, MSVC 8.0 doesn't seem to have a single-threaded
> version of the CRT for me to test against that!)
>
> -yzt
>
> -- 
> "Programming is an art that fights back!"
> _______________________________________________
> Winpcap-users mailing list
> Winpcap-users at winpcap.org
> https://www.winpcap.org/mailman/listinfo/winpcap-users 



More information about the Winpcap-users mailing list