[Winpcap-users] pcap_freealldevs() causing program crash.

Guy Harris guy at alum.mit.edu
Wed Nov 16 20:29:21 GMT 2005


Eric Hansen wrote:

> What I do, is store the info like so:
> 
> cDevice[iCount] = d->name;

In C, that's not copying a string.  That's copying the pointer *to* the 
string.

Even in C++, it doesn't copy the string, it copies the pointer to the 
string - unless somehow operator overloading works on "char *", causing 
a copy of the string to be allocated and the copy assigned to 
cDevice[iCount].

Unless somehow that operation causes a copy of the string pointed to by 
d->name to be made, and a pointer to that *copy* assigned to 
cDevice[iCount], then cdevice[iCount] will *NOT* be valid after 
pcap_freealldevs() is called.

Try doing

	cDevice[iCount] = strdup(d->name);

instead (make sure you include <string.h> to declare strdup()).

(BTW, presumably you're growing the array if iCount is larger than its 
current size, or quitting once iCount reaches the size of the array, right?)

> Then, when I goto list the info, I do this:
> 
> for(i = 0; i < iCount; i++){
> m_devices.AddString(cDevice[i]);
> }

Are you doing that *before* calling pcap_freealldevs()?  If not, that 
won't work unless you're using strdup() to copy d->name (or whatever 
language environment you're using does that, or the equivalent of that, 
for you).


More information about the Winpcap-users mailing list