Deallocating memory in isc code.

kalpesh varyani kalpesh.link at gmail.com
Wed Jun 8 13:19:07 UTC 2011


Hi,

Is it necessary to deallocate memory assigned using isc_mem_get() by
explicitly using isc_mem_free()?

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 (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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.isc.org/pipermail/bind-users/attachments/20110608/701fc3a0/attachment.html>


More information about the bind-users mailing list