首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用redisAsyncCommand()时出现分段错误

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

Stack Overflow用户
提问于 2017-06-26 06:42:55
回答 1查看 415关注 0票数 0

我有下一个简单的代码:

代码语言:javascript
复制
    #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;
}

程序编译运行良好。但它在执行时会返回错误“分割错误”。如果我注释redisAsyncCommand()函数,错误就会消失。我的代码中的错误在哪里?

添加:

当我编译我的代码时,我得到了警告:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2017-06-26 14:11:51

编辑

主要问题似乎在包含的适配器文件中。

我指出的问题是无关的。

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

代码语言:javascript
复制
if (r->type == REDIS_REPLY_ARRAY) {
    for (j = 0; j < r->elements; j++) {
        printf("%u) %s\n", j, r->element[j]->str);
    }
}

您的代码假定应答是一个字符串数组。

这是不正确。

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

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

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

祝好运!

附注:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44751252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档