Redis 6.0+ introduced PUSH replies (reply-type >) which are generated spontaneously and can arrive at any time. Because they are asynchronous, they must be handled using callbacks.
By default, hiredis installs handlers on redisContext and redisAsyncContext that intercept and automatically free these replies. If you need custom logic, you must provide a callback.
Important Memory Management Rules:
- For
redisContext (blocking): You must call freeReplyObject(reply) inside your custom handler to prevent memory leaks. - For
redisAsyncContext (async): You must NOT call freeReplyObject(reply) because the async engine handles freeing automatically.
/* redisContext callback prototype */
void my_push_handler(void *privdata, void *reply) {
/* Handle the reply */
freeReplyObject(reply);
}
/* redisAsyncContext callback prototype */
void my_async_push_handler(redisAsyncContext *ac, void *reply) {
/* Handle the reply */
/* DO NOT call freeReplyObject(reply) here */
}