From 9e417047edb4e95dcffbd8827f0f6a4f1fcdab51 Mon Sep 17 00:00:00 2001 From: Pieter Noordhuis Date: Sat, 25 Sep 2010 15:33:27 +0200 Subject: Add function to free an allocated context --- hiredis.c | 11 +++++++++++ hiredis.h | 1 + test.c | 8 +++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/hiredis.c b/hiredis.c index 187cdd0..29968c9 100644 --- a/hiredis.c +++ b/hiredis.c @@ -586,6 +586,17 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) { return c; } +void redisFree(redisContext *c) { + if (c->error != NULL) + sdsfree(c->error); + if (c->obuf != NULL) + sdsfree(c->obuf); + if (c->clen > 0) + free(c->callbacks); + redisReplyReaderFree(c->reader); + free(c); +} + /* Connect to a Redis instance. On error the field error in the returned * context will be set to the return value of the error function. * When no set of reply functions is given, the default set will be used. */ diff --git a/hiredis.h b/hiredis.h index 15b4f61..96508ad 100644 --- a/hiredis.h +++ b/hiredis.h @@ -103,6 +103,7 @@ int redisReplyReaderGetReply(void *reader, void **reply); redisContext *redisConnect(const char *ip, int port, redisReplyFunctions *fn); redisContext *redisConnectNonBlock(const char *ip, int port, redisReplyFunctions *fn); +void redisFree(redisContext *c); int redisBufferRead(redisContext *c); int redisBufferWrite(redisContext *c, int *done); int redisGetReply(redisContext *c, void **reply); diff --git a/test.c b/test.c index fed149e..58772fc 100644 --- a/test.c +++ b/test.c @@ -28,16 +28,17 @@ int main(void) { int i, ret, tests = 0, fails = 0; long long t1, t2; redisContext *c; - redisReply *reply; + redisReply *reply, **replies; void *reader; char *err; - __connect(&c); + __connect(&c); test("Returns I/O error when the connection is lost: "); test_cond(redisCommand(c,"QUIT") == NULL && strcmp(c->error,"Server closed the connection") == 0); - __connect(&c); /* reconnect */ + redisFree(c); + __connect(&c); /* reconnect */ test("Is able to deliver commands: "); reply = redisCommand(c,"PING"); test_cond(reply->type == REDIS_REPLY_STRING && @@ -170,5 +171,6 @@ int main(void) { printf("*** %d TESTS FAILED ***\n", fails); } + redisFree(c); return 0; } -- cgit v1.2.3