c - 使用 redisAsyncCommand() 时出现段错误

标签 c redis

我有下一个简单的代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <signal.h>
    #include "hiredis.h"
    #include "async.h"
    #include "adapters/libevent.h"

void onMessage(redisAsyncContext *c, void *reply, void *privdata) {
    redisReply *r = reply;
    if (reply == NULL) return;

    int j;

    if (r->type == REDIS_REPLY_ARRAY) {
        for (j = 0; j < r->elements; j++) {
            printf("%u) %s\n", j, r->element[j]->str);
        }
    }*/
}


int main (int argc, char **argv) {

    signal(SIGPIPE, SIG_IGN);
    struct event_base *base = event_base_new();

    redisAsyncContext *c = redisAsyncConnect("127.0.0.1", 6379);
    if (c->err) {
        printf("error: %s\n", c->errstr);
        return 1;
    }

    redisLibeventAttach(c, base);
    redisAsyncCommand(c, onMessage, NULL, "SUBSCRIBE messages");
    event_base_dispatch(base);
    return 0;
}

程序编译并运行良好。但它在执行时返回错误“Segmentation fault”。如果我评论 redisAsyncCommand() 函数错误消失。 我的代码哪里出错了?

添加:

当我编译我的代码时,我有警告:

make all 
'Building file: ../src/redis.c'
'Invoking: Cross GCC Compiler'
arm-linux-gnueabihf-gcc -I"C:\SysGCC\Raspberry\include" -I"C:\SysGCC\Raspberry\include\hiredis" -I"C:\SysGCC\Raspberry\include\event2" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/redis.d" -MT"src/redis.o" -o "src/redis.o" "../src/redis.c"
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventAddRead':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:56:15: warning: passing argument 1 of 'event_add' from incompatible pointer type
     event_add(&e->rev,NULL);
               ^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
                 from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:937:5: note: expected 'struct event *' but argument is of type 'struct event **'
 int event_add(struct event *ev, const struct timeval *timeout);
     ^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventDelRead':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:61:15: warning: passing argument 1 of 'event_del' from incompatible pointer type
     event_del(&e->rev);
               ^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
                 from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:950:5: note: expected 'struct event *' but argument is of type 'struct event **'
 int event_del(struct event *);
     ^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventAddWrite':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:66:15: warning: passing argument 1 of 'event_add' from incompatible pointer type
     event_add(&e->wev,NULL);
               ^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
                 from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:937:5: note: expected 'struct event *' but argument is of type 'struct event **'
 int event_add(struct event *ev, const struct timeval *timeout);
     ^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventDelWrite':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:71:15: warning: passing argument 1 of 'event_del' from incompatible pointer type
     event_del(&e->wev);
               ^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
                 from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:950:5: note: expected 'struct event *' but argument is of type 'struct event **'
 int event_del(struct event *);
     ^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventCleanup':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:76:15: warning: passing argument 1 of 'event_del' from incompatible pointer type
     event_del(&e->rev);
               ^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
                 from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:950:5: note: expected 'struct event *' but argument is of type 'struct event **'
 int event_del(struct event *);
     ^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:77:15: warning: passing argument 1 of 'event_del' from incompatible pointer type
     event_del(&e->wev);
               ^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
                 from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:950:5: note: expected 'struct event *' but argument is of type 'struct event **'
 int event_del(struct event *);
     ^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h: In function 'redisLibeventAttach':
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:102:5: warning: implicit declaration of function 'event_set' [-Wimplicit-function-declaration]
     event_set(&e->rev,c->fd,EV_READ,redisLibeventReadEvent,e);
     ^
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:104:25: warning: passing argument 2 of 'event_base_set' from incompatible pointer type
     event_base_set(base,&e->rev);
                         ^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
                 from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:627:5: note: expected 'struct event *' but argument is of type 'struct event **'
 int event_base_set(struct event_base *, struct event *);
     ^
In file included from ../src/redis.c:7:0:
C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:105:25: warning: passing argument 2 of 'event_base_set' from incompatible pointer type
     event_base_set(base,&e->wev);
                         ^
In file included from C:\SysGCC\Raspberry\include\hiredis/adapters/libevent.h:33:0,
                 from ../src/redis.c:7:
C:\SysGCC\Raspberry\include\event2/event.h:627:5: note: expected 'struct event *' but argument is of type 'struct event **'
 int event_base_set(struct event_base *, struct event *);
     ^
'Finished building: ../src/redis.c'
' '
'Building target: redis'
'Invoking: Cross GCC Linker'
arm-linux-gnueabihf-gcc -L"C:\SysGCC\Raspberry\arm-linux-gnueabihf\sysroot\usr\local\lib" -L"C:\SysGCC\Raspberry\arm-linux-gnueabihf\lib" -o "redis"  ./src/redis.o   -lhiredis -levent_core -lpthread
'Finished building target: redis'
' '

00:20:55 Build Finished (took 1s.130ms)

最佳答案

编辑

看来主要问题出在包含的适配器文件中。

我指出的问题无关。


这只是我注意到的一个小问题:

if (r->type == REDIS_REPLY_ARRAY) {
    for (j = 0; j < r->elements; j++) {
        printf("%u) %s\n", j, r->element[j]->str);
    }
}

您的代码假定回复是一个字符串数组

这是不正确的。

订阅命令还返回一个数组响应,该响应的最后一个成员是数字(不是字符串)。

因此,假设它是一个字符串数组将失败。

我不确定这是否是原因,但这可能是一个重要问题。

祝你好运!


附言

我可能会无耻地在这里推广我自己的项目,但您可能会喜欢使用 facil.io's integrated Redis Pub/Sub engine .如果您需要 Websocket 或 HTTP 支持,它可能会为您节省大量编码。

关于c - 使用 redisAsyncCommand() 时出现段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44751252/

相关文章:

ruby-on-rails - 静态 : Creating a solution for document rating (Redis/Rails/NodeJS ? )

node.js - 无法在 sailsjs 中获取 redis session

c - 读取文本文件并用分隔符分隔它

c - 使用 ASCII 字符和进行二进制搜索字符串?

c - 如何解决 printf 中的 %d 输出显示 num1、num2 和 sum 的 ascii 值,而不是它们分配的值

c - 简单的浮点乘法未给出预期结果

c++ - qsort() vs std::sort,比较函数哲学差异

redis - Redis 中的请求合并

ruby-on-rails - Ruby On Rails,Redis::CommandError: 'set' 命令的 ERR 参数数量错误

database - 从redis中的多键排序集中查询数据