Deallocating memory in isc code.

kalpesh varyani kalpesh.link at gmail.com
Thu Jun 9 04:00:00 UTC 2011


Thanks for the information. The reason I asked these questions is that after
incorporating these changes in builtin.c, memory leak is bein reported.
Memory leak goes away on reverting the changes from builtin.c file.

>
> 1. Even if *builtin_t *empty; *is local to the function *builtin_create*,
can
> it cause memory leak?

> 2. what should be the rerurn value of *isc_mem_strdup()* on
success/failure?
>
> Regards,
> Kalpesh


On Wed, Jun 8, 2011 at 7:32 PM, Mark Andrews <marka at isc.org> wrote:

>
> In message <BANLkTi=DW4SsSphHF5MJ3QV4ofSquTKkGA at mail.gmail.com>, kalpesh
> varyani writes:
> > Hi,
> >
> > Is it necessary to deallocate memory assigned using isc_mem_get() by
> > explicitly using isc_mem_free()?
>
> No.  You use isc_mem_put().  See <isc/mem.h>
>
> > Ref: File builtin.c is available at
> > ftp://1node.net/linux/bind-9.7.0b1/bin/named/*builtin*.*c*<
> ftp://1node.net/linux/bind-9.7.0b1/bin/named/builtin.c>
> >
> > Following is the code snippet:
> >
> > static isc_result_t builtin_create(const char *zone, int argc, char
> > **argv,void *driverdata, void **dbdata)
> > {
> > ...
> > builtin_t *empty; <-- Local variable.
> > ...
> > empty = *isc_mem_get*(ns_g_mctx, sizeof(*empty)); <-- Memory assigned
> using
> > isc_mem_get
> > server = isc_mem_strdup(ns_g_mctx, argv[1]);
> > contact = isc_mem_strdup(ns_g_mctx, argv[2]);
>
> If any of the allocations failed then free all that succeeded.
> Return the address of empty_builtin in that case.
>
> > if (empty == NULL || server == NULL || contact == NULL) {
> >     *dbdata = &empty_builtin;
> >     if (server != NULL)
> >         isc_mem_free(ns_g_mctx, server);
> >     if (contact != NULL)
> >         isc_mem_free(ns_g_mctx, contact);
> >     if (empty != NULL)
> >         isc_mem_put(ns_g_mctx, empty, sizeof (*empty));
> > }
> > ...
> > }
> >
> > Here memory is assigned using isc_mem_get() however isc_mem_free() is
> > conditional. Thus if conditions are false then there will be no
> > isc_mem_free().
> >
> > 1. Even if *builtin_t *empty; *is local to the function *builtin_create*,
> can
> > it cause memory leak?
>
> > 2. what should be the rerurn value of *isc_mem_strdup()* on
> success/failure?
> >
> > Regards,
> > Kalpesh
> >
> > --20cf303f6bc668d58504a5332e6c
> > Content-Type: text/html; charset=ISO-8859-1
> > Content-Transfer-Encoding: quoted-printable
> >
> > <div>Hi,</div>
> > <div>=A0</div>
> > <div>Is it necessary to deallocate memory assigned using isc_mem_get() by
> e=
> > xplicitly using isc_mem_free()?</div>
> > <div>=A0</div>
> > <div>Ref: File=A0builtin.c is available at <a href=3D"
> ftp://1node.net/linux=
> > /bind-9.7.0b1/bin/named/builtin.c"><font color=3D"#0e774a">
> ftp://1node.net/=
> > linux/bind-9.7.0b1/bin/named/<b>builtin</b>.<b>c</b></font></a></div>
> > <div>=A0</div>
> > <div>Following is the code snippet:</div>
> > <div><span lang=3D"EN"></span>=A0</div>
> > <div><span lang=3D"EN">static isc_result_t builtin_create(const char
> *zone,=
> >  int argc, char **argv,void *driverdata, void **dbdata) </span></div>
> > <div><span lang=3D"EN">{</span></div>
> > <div><span lang=3D"EN">...<br></span><span lang=3D"EN"><span
> lang=3D"EN">bu=
> > iltin_t *empty; <-- Local variable.</span></span></div>
> > <div><span lang=3D"EN"><span lang=3D"EN">...</span><br></span><span
> lang=3D=
> > "EN"><span lang=3D"EN">empty =3D <strong>isc_mem_get</strong>(ns_g_mctx,
> si=
> > zeof(*empty)); <-- Memory assigned using isc_mem_get<br>server =3D
> isc_m=
> > em_strdup(ns_g_mctx, argv[1]);<br>
> > contact =3D isc_mem_strdup(ns_g_mctx, argv[2]);<br>if (empty =3D=3D NULL
> ||=
> >  server =3D=3D NULL || contact =3D=3D NULL) {<br>=A0=A0=A0 *dbdata =3D
> &amp=
> > ;empty_builtin;<br>=A0=A0=A0 if (server !=3D
> NULL)=A0=A0=A0=A0<br>=A0=A0=A0=
> > =A0=A0=A0=A0 isc_mem_free(ns_g_mctx, server);<br>
> > =A0=A0=A0 if (contact !=3D NULL)<br>=A0=A0=A0=A0=A0=A0=A0
> isc_mem_free(ns_g=
> > _mctx, contact);<br>=A0=A0=A0 if (empty !=3D
> NULL)<br>=A0=A0=A0=A0=A0=A0=A0=
> >  isc_mem_put(ns_g_mctx, empty, sizeof (*empty));<br>} </span></span><span
> l=
> > ang=3D"EN"><span lang=3D"EN"></span></span></div>
> >
> > <div><span lang=3D"EN">...</span></div>
> > <div><span lang=3D"EN">}</span></div>
> > <div><span lang=3D"EN"></span>=A0</div>
> > <div><span lang=3D"EN">Here memory is assigned using isc_mem_get()
> however =
> > isc_mem_free() is conditional. Thus if conditions are false then there
> will=
> >  be no isc_mem_free().</span></div>
> > <div><span lang=3D"EN"></span>=A0</div>
> > <div><span lang=3D"EN">1. Even if <span lang=3D"EN"><strong>builtin_t
> *empt=
> > y; </strong>is local to the function
> <strong>builtin_create</strong>,=A0can=
> >  it cause memory leak?</span></span></div>
> > <div><span lang=3D"EN"><span lang=3D"EN">2. what should be the rerurn
> value=
> >  of <strong>isc_mem_strdup()</strong> on
> success/failure?</span></span></di=
> > v>
> > <div><span lang=3D"EN"><span lang=3D"EN"></span></span>=A0</div>
> > <div><span lang=3D"EN"><span lang=3D"EN">Regards,</span></span></div>
> > <div><span lang=3D"EN"><span lang=3D"EN">Kalpesh</span></span></div>
> >
> > --20cf303f6bc668d58504a5332e6c--
> >
> > --===============3205411032284859244==
> > Content-Type: text/plain; charset="us-ascii"
> > MIME-Version: 1.0
> > Content-Transfer-Encoding: 7bit
> > Content-Disposition: inline
> >
> > _______________________________________________
> > bind-users mailing list
> > bind-users at lists.isc.org
> > https://lists.isc.org/mailman/listinfo/bind-users
> > --===============3205411032284859244==--
> --
> Mark Andrews, ISC
> 1 Seymour St., Dundas Valley, NSW 2117, Australia
> PHONE: +61 2 9871 4742                 INTERNET: marka at isc.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.isc.org/pipermail/bind-users/attachments/20110609/0110377f/attachment.html>


More information about the bind-users mailing list