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(a)redhat.com +1-919-301-3266
Libvirt virtualization library
http://libvirt.org