[Winpcap-users] Problem Freeing Array Returned bypcap_list_datalinks

Steighton_Haley at McAfee.com Steighton_Haley at McAfee.com
Mon Sep 11 19:06:57 GMT 2006


> 
> By the way, do you have any idea whether this is a real 
> problem (i.e. the memory won't be correctly released) or it's 
> just something that the VC memory leak checker would complain 
> because it has no track of the block's allocation?

Yes it's a real problem.  Mainly because of the heap management done by
the C runtime library.  The memory manager in the CRT will attempt to
free the memory, and an exception will be generated because the area of
memory that is referenced is not owned by the CRT for the module.  The
memory will not be freed, even if you catch the exception.

This particular problem is well documented on Microsoft's MSDN site.

> 
> What if the library provides it's own free() function? This 
> can be a simple call to CRT's free() (which would be the 
> correct one,) and could be used on all memory allocated 
> inside the library and returned for the caller to free later. 
> But I do agree that there are already too many memory 
> allocation/deallocation functions out there (specially in 
> Win32 API) and no one needs another to add to the mess. (I 
> can imagine a situation in a big application, that each block 
> of memory would have a pointer to its correct free() function 
> along with it, because it uses a great number of different 
> libraries that make use of this strategy!)

This is a fine strategy used by many programmers.  Although it's much
less hassle to make sure all of your modules are built with the DLL
version of the CRT.  In that case (since the CRT is in the same DLL),
all modules are sharing the same CRT, so it doesn't matter where the
memory is freed because all memory management is handled by the same CRT
module.

The catch there is that you need to make sure that the correct
MSVCRT.dll is installed on the target system where your application is
going to run.

> I guess there's no escaping having to call a function such as 
> this twice (or two different functions) once to get the size 
> of the required buffer, and again to actually get the data, 
> you know like some Win32 functions.

Nope, not if they have different versions of the CRT.

---
Steighton Haley                          shaley at mcafee.com
Software Engineer

"Why do nerds confuse Halloween and Christmas?  Because OCT31=DEC25"



More information about the Winpcap-users mailing list