From: Brian Aker Date: Thu, 20 Sep 2007 13:59:52 +0000 (-0700) Subject: Memory leak fixed. X-Git-Tag: 0.2~17 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=be683ec76c3d55c427d72d3529f521e479967f6b;p=m6w6%2Flibmemcached Memory leak fixed. --- diff --git a/lib/memcached.c b/lib/memcached.c index 3cbac0b2..c636b9b4 100644 --- a/lib/memcached.c +++ b/lib/memcached.c @@ -32,11 +32,9 @@ void memcached_deinit(memcached_st *ptr) { for (x= 0; x < ptr->number_of_hosts; x++) { - if (ptr->hosts[x].fd > 0) + if (ptr->hosts[x].fd == -1) close(ptr->hosts[x].fd); - - if (ptr->hosts[x].hostname) - free(ptr->hosts[x].hostname); + free(ptr->hosts[x].hostname); } free(ptr->hosts); diff --git a/lib/memcached_connect.c b/lib/memcached_connect.c index 1be786e1..31f8ce16 100644 --- a/lib/memcached_connect.c +++ b/lib/memcached_connect.c @@ -9,9 +9,11 @@ memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigne hostname= "localhost"; - ptr->hosts= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (size_t)ptr->number_of_hosts); + ptr->hosts= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (ptr->number_of_hosts+1)); + memset(ptr->hosts, 0, sizeof(memcached_host_st) * (size_t)ptr->number_of_hosts); ptr->hosts[ptr->number_of_hosts].hostname= - (char *)malloc(sizeof(char) * strlen(hostname)); + (char *)malloc(sizeof(char) * (strlen(hostname) + 1)); + memset(ptr->hosts[ptr->number_of_hosts].hostname, 0, sizeof(char) * (strlen(hostname) + 1)); memcpy(ptr->hosts[ptr->number_of_hosts].hostname, hostname, strlen(hostname)); ptr->hosts[ptr->number_of_hosts].port= port; ptr->hosts[ptr->number_of_hosts].fd= -1; diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 2dcdcccd..c688af34 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -10,6 +10,7 @@ char *memcached_get(memcached_st *ptr, char *key, size_t key_length, char *string_ptr; unsigned int server_key; + *value_length= 0; *error= memcached_connect(ptr); if (*error != MEMCACHED_SUCCESS) diff --git a/tests/test.c b/tests/test.c index 020bfc53..784ec9ae 100644 --- a/tests/test.c +++ b/tests/test.c @@ -23,6 +23,17 @@ void allocation_test(void) memcached_deinit(memc); } +void connection_test(void) +{ + memcached_return rc; + memcached_st *memc; + memc= memcached_init(NULL); + rc= memcached_server_add(memc, "localhost", 0); + assert(rc == MEMCACHED_SUCCESS); + assert(memc); + memcached_deinit(memc); +} + void set_test(void) { memcached_st *memc; @@ -122,6 +133,8 @@ void get_test(void) string= memcached_get(memc, key, strlen(key), &string_length, &flags, &rc); + printf("RC %u\n", rc); + assert(rc == MEMCACHED_NOTFOUND); assert(string_length == 0); assert(!string); @@ -228,6 +241,7 @@ int main(void) init_test(); allocation_test(); + connection_test(); set_test(); add_test(); replace_test();