使用redisAsyncCommand()时出现分段错误

时间:2017-06-25 22:42:55

标签: 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)

1 个答案:

答案 0 :(得分:0)

修改

似乎主要问题出在附带的适配器文件中。

我指出的问题是无关的。

这是我注意到的一个快速问题:

 public FamilyPage LoginPage(string StudentID,string StudentBirthDate,string ParentID)
    {
        _parentID.SendKeys(ParentID);
        _birthDate.SendKeys(StudentBirthDate);
        _studentID.SendKeys(StudentID);
        _buttonSubmit.Click();
        return new FamilyPage(_webdriver);
    }

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

不正确。

Subscribe命令还返回一个Array响应,该响应的最后一个成员是NUMBER(不是字符串)。

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

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

祝你好运!

P.S。

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