【问题标题】:hiredis redisCommand returns null for everything on Raspberry Pi 4hiredis redisCommand 为 Raspberry Pi 4 上的所有内容返回 null
【发布时间】:2020-04-18 06:57:55
【问题描述】:

我有带有最新 raspbian 和更新软件的 Raspberry pi 4B。我使用他们的安装说明从 github 源安装了hiredis 库。当我尝试在普通计算机上运行以下代码时,一切正常,但在 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;
}

编译:gcc redis-test-rw.c -o redis-test-rw -g -lhiredis

运行:

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 上,hiredis 没有问题,而在计算机上也没问题。

感谢您的提前!

编辑: 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)

【问题讨论】:

  • 当您MONITORredis-cli 中的数据库并执行代码时会发生什么?只是为了断言在我的笔记本上你的代码也可以正常工作。
  • @Niloct 这是由MONITOR 打印的:1577573389.883836 [0 127.0.0.1:47228] "SET" "test" "Hello World"
  • 这有点奇怪,因为出现错误时replyNULL,而不是reply-&gt;strNULLgithub.com/redis/hiredis/blob/master/hiredis.c#L1060
  • 所以reply-&gt;strreplyNULL 应该是一个段错误。
  • 你是如何在 4B 上安装hiredis的?

标签: c arm hiredis raspberry-pi4


【解决方案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:

【讨论】:

  • 并监控它?
  • 监视器看起来正确:1577577229.982302 [0 127.0.0.1:47586] "PING" "Hello World"
  • 清除你的hiredis安装,下载github.com/redis/hiredis/archive/v0.13.3.zip并编译并尝试这个版本。
  • 哦,我没有使用发布版本,而是来自 master 分支的版本。 v0.14 也很好用,非发布版本可能有错误。现在一切都像魅力一样,非常感谢您的帮助!
猜你喜欢
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2013-06-30
相关资源
最近更新 更多