Add function to free an allocated context

This commit is contained in:
Pieter Noordhuis 2010-09-25 15:33:27 +02:00
parent cab99f6427
commit 9e417047ed
3 changed files with 17 additions and 3 deletions

View File

@ -586,6 +586,17 @@ static redisContext *redisContextInit(redisReplyFunctions *fn) {
return c; 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 /* 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. * 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. */ * When no set of reply functions is given, the default set will be used. */

View File

@ -103,6 +103,7 @@ int redisReplyReaderGetReply(void *reader, void **reply);
redisContext *redisConnect(const char *ip, int port, redisReplyFunctions *fn); redisContext *redisConnect(const char *ip, int port, redisReplyFunctions *fn);
redisContext *redisConnectNonBlock(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 redisBufferRead(redisContext *c);
int redisBufferWrite(redisContext *c, int *done); int redisBufferWrite(redisContext *c, int *done);
int redisGetReply(redisContext *c, void **reply); int redisGetReply(redisContext *c, void **reply);

8
test.c
View File

@ -28,16 +28,17 @@ int main(void) {
int i, ret, tests = 0, fails = 0; int i, ret, tests = 0, fails = 0;
long long t1, t2; long long t1, t2;
redisContext *c; redisContext *c;
redisReply *reply; redisReply *reply, **replies;
void *reader; void *reader;
char *err; char *err;
__connect(&c);
__connect(&c);
test("Returns I/O error when the connection is lost: "); test("Returns I/O error when the connection is lost: ");
test_cond(redisCommand(c,"QUIT") == NULL && test_cond(redisCommand(c,"QUIT") == NULL &&
strcmp(c->error,"Server closed the connection") == 0); strcmp(c->error,"Server closed the connection") == 0);
__connect(&c); /* reconnect */ redisFree(c);
__connect(&c); /* reconnect */
test("Is able to deliver commands: "); test("Is able to deliver commands: ");
reply = redisCommand(c,"PING"); reply = redisCommand(c,"PING");
test_cond(reply->type == REDIS_REPLY_STRING && test_cond(reply->type == REDIS_REPLY_STRING &&
@ -170,5 +171,6 @@ int main(void) {
printf("*** %d TESTS FAILED ***\n", fails); printf("*** %d TESTS FAILED ***\n", fails);
} }
redisFree(c);
return 0; return 0;
} }