[libvirt] [PATCHv3] network: Avoid memory leaks on networkBuildDnsmasqArgv

Eric Blake eblake at redhat.com
Wed Feb 1 00:23:09 UTC 2012

On 01/31/2012 12:15 AM, Alex Jia wrote:
>> What I'd suggest is:
>> declare variables outside the loop,
>> start the for loop by freeing any state from previous iterations,
>> and also free all state in the cleanup label
>> at which point, you _don't_ have to follow the v1 approach of trying to
>> free variables before every goto or break (and missing some).  Something
>> like:
>> char *record = NULL;
>> for (i = 0; i<  dns->nsrrvrecords; i++) {
>>      /* free previous iteration */
>>      VIR_FREE(record);
> Hi Eric, thanks for your nice comment, the 'record' variable is
> allocated memory in previous for loop,
> but we free it in here, the issue is 'dns->ntxtrecords' =
> 'dns->nsrrvrecord' ? if not, is it also okay?

That's why you _also_ free things in the cleanup label.

If every iteration of the loop frees the previous iteration, then the
first iteration of the loop is a no-op, and the cleanup code frees the
last iteration, no matter whether you get to the cleanup code via a goto
or whether the last iteration exited normally.

Eric Blake   eblake at redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 620 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20120131/a7523a2c/attachment-0001.sig>

More information about the libvir-list mailing list