开发者社区> 问答> 正文

hiredis redisCommand为Raspberry Pi 4上的所有内容返回null

我的Raspberry pi 4B具有最新的raspbian和更新的软件。我hiredis使用它们的安装说明从github源安装了库。当我尝试在普通计算机上运行以下代码时,一切正常,但在Raspberry Pi 4B上redisCommand始终返回null。当我使用SET命令时,数据库被更新。

码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <hiredis/hiredis.h>


int main (int argc, char **argv) {
    redisReply *reply;
    redisContext *c;

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


    /* Set a key */
    reply = redisCommand(c,"SET %s %s", "test", "Hello World");
    printf("SET: %s\n", reply->str);
    printf("error: %s\n", c->errstr);
    freeReplyObject(reply);

    redisFree(c);
    return 0;
}

汇编: ```js gcc redis-test-rw.c -o redis-test-rw -g -lhiredis


跑:

```js
pi@rpi:~ $ ./redis-test-rw 
SET: (null)
error:

pi@rpi:~ $ 在此调用后,Redis:

pi@rpi:~ $ redis-cli 
127.0.0.1:6379> GET test
"Hello World"
127.0.0.1:6379>

pi@rpi:~ $ Redis MONITOR命令:

127.0.0.1:6379> MONITOR
OK
1577573389.883836 [0 127.0.0.1:47228] "SET" "test" "Hello World"

我对此感到非常困惑,因为在以前的Raspberry pi 3B上,hireddis没有问题,在计算机上也没问题。

感谢前进!

编辑:Valgrind报告:

$ valgrind ./redis-test-rw 
==21969== Memcheck, a memory error detector
==21969== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==21969== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==21969== Command: ./redis-test-rw
==21969== 
==21969== Invalid read of size 8
==21969==    at 0x4865004: ??? (in /usr/lib/arm-linux-gnueabihf/libarmmem-v7l.so)
==21969==  Address 0x49f964c is 36 bytes inside a block of size 42 alloc'd
==21969==    at 0x4847690: malloc (vg_replace_malloc.c:309)
==21969==    by 0x48907BB: redisvFormatCommand (in /usr/lib/arm-linux-gnueabihf/libhiredis.so.0.14)
==21969== 
SET: (null)
error: 
==21969== 
==21969== HEAP SUMMARY:
==21969==     in use at exit: 0 bytes in 0 blocks
==21969==   total heap usage: 23 allocs, 23 frees, 1,913 bytes allocated
==21969== 
==21969== All heap blocks were freed -- no leaks are possible
==21969== 
==21969== For lists of detected and suppressed errors, rerun with: -s
==21969== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

展开
收起
几许相思几点泪 2019-12-29 20:56:34 7267 0
1 条回答
写回答
取消 提交回答
  • 编译此示例代码并验证结果:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <hiredis/hiredis.h>
    
    int main (int argc, char **argv) {
        redisReply *reply;
        redisContext *c;
    
        c = redisConnect("127.0.0.1", 6379);
        if (c->err) {
            printf("error: %s\n", c->errstr);
            return 1;
        }
    
        /* PINGs */
        reply = redisCommand(c,"PING %s", "Hello World");
        printf("RESPONSE: %s\n", reply->str);
        printf("error: %s\n", c->errstr);
        freeReplyObject(reply);
    
        redisFree(c);
        return 0;
    }
    
    

    应打印:

    RESPONSE: Hello World
    error:
    
    2019-12-29 20:56:58
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载