summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPieter Noordhuis <pcnoordhuis@gmail.com>2010-09-25 15:33:27 +0200
committerPieter Noordhuis <pcnoordhuis@gmail.com>2010-09-25 15:33:27 +0200
commit9e417047edb4e95dcffbd8827f0f6a4f1fcdab51 (patch)
tree742dbc81808b51cf965ddad56a03f69dffc6fed8
parentcab99f6427f5255b543592c429c899aeef866352 (diff)
Add function to free an allocated context
-rw-r--r--hiredis.c11
-rw-r--r--hiredis.h1
-rw-r--r--test.c8
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;
}