我尝试获取0mq消息的单个部分,它的工作方式与预期的一样。 然而,如果我用valgrind测试程序,我得到:
== 9436 ==条件跳转或移动取决于未初始化的值
== 9436 ==在0x4C2E128:strlen(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)
== 9436 == by 0x401023:main(werft.c:73)
== 9436 ==
== 9436 ==条件跳转或移动取决于未初始化的值
== 9436 ==在0x4C2E128:strlen(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)
== 9436 == by 0x401048:main(werft.c:74)
== 9436 ==
== 9436 ==条件跳转或移动取决于未初始化的值
== 9436 ==在0x4C2E128:strlen(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)
== 9436 == by 0x538B7F9:vfprintf(在/usr/lib/libc-2.25.so中)
== 9436 == by 0x53B246F:vsnprintf(在/usr/lib/libc-2.25.so中)
== 9436 == by 0x5391F5E:snprintf(在/usr/lib/libc-2.25.so中)
== 9436 == by 0x40106F:main(werft.c:74)
我的代码是:
#define DEVICE_IDENTITY "de.logicway.pferdepflug"
int main()
{
char *incoming[2];
void *context = zmq_ctx_new();
void *responder = zmq_socket(context, ZMQ_ROUTER);
zmq_bind(responder, "tcp://127.0.0.1:9990");
void *requester = zmq_socket(context, ZMQ_DEALER);
zmq_setsockopt(requester, ZMQ_IDENTITY, DEVICE_IDENTITY, strlen(DEVICE_IDENTITY));
zmq_connect(requester, "tcp://127.0.0.1:9990");
zmq_send(requester, "testfahrt", 9, 0);
zmq_msg_t received;
zmq_msg_init(&received);
int i = 0;
do {
int size = zmq_msg_recv(&received, responder, 0);
char *buffer = (char*) zmq_msg_data(&received);
incoming[i] = (char*) malloc(strlen(buffer) +1 * sizeof(char*));
snprintf(incoming[i], strlen(buffer) +1, "%s", buffer);
i++;
} while(zmq_msg_more(&received));
printf("Identifier: %s\n", incoming[0]);
printf("Message: %s\n", incoming[1]);
free(incoming[0]);
free(incoming[1]);
zmq_msg_close(&received);
zmq_close(requester);
zmq_close(responder);
zmq_ctx_destroy(context);
return 0;
}
(这不会像现在这样投入生产,只是让我理解0mq)
由于代码有效,并且#34;罚款"实际上,单位化值可能没有问题。但是,它仍困扰着我。
问题/ S:
非常感谢您的帮助!
答案 0 :(得分:1)
Valgrind提到strlen
正在使用未初始化的内存。在您的情况下,参数是buffer
,它是从zmq库中检索的。
也许你应该检查所涉及的函数调用的返回值。
编辑:我使用了错误的API函数。删除了这部分。正如已经提到的,你也搞砸了你的分配:
incoming[i] = (char*) malloc(strlen(buffer) +1 * sizeof(char*));
您不需要char *
类型的元素,只需要char
。 +1
属于长度:
incoming[i] = (char*) malloc((strlen(buffer) +1) * sizeof(char));
鉴于sizeof (char)
被定义为1且规则不能转换malloc
的返回值,我们得到了这个:
incoming[i] = malloc(strlen(buffer) + 1);
下一步是从buffer
复制字符串:
snprintf(incoming[i], strlen(buffer) +1, "%s", buffer);
鉴于您刚刚分配了足够的内存(可能会添加NULL
检查),使用snprintf
并没有太大的好处。
您只需使用strcpy
或只需一步创建strdup
的副本
答案 1 :(得分:0)
看起来这是ZeroMQ错误。我的输出有--track-origins=yes
valgrind选项:
[ ~]$ valgrind --track-origins=yes ./a.out
==5272== Memcheck, a memory error detector
==5272== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==5272== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==5272== Command: ./a.out
==5272==
==5272== Conditional jump or move depends on uninitialised value(s)
==5272== at 0x4C30BC8: strlen (vg_replace_strmem.c:454)
==5272== by 0x400C52: main (111.c:31)
==5272== Uninitialised value was created by a stack allocation
==5272== at 0x4E860B0: zmq::stream_engine_t::mechanism_ready() (stream_engine.cpp:793)
==5272==
==5272== Conditional jump or move depends on uninitialised value(s)
==5272== at 0x4C30BC8: strlen (vg_replace_strmem.c:454)
==5272== by 0x400C77: main (111.c:32)
==5272== Uninitialised value was created by a stack allocation
==5272== at 0x4E860B0: zmq::stream_engine_t::mechanism_ready() (stream_engine.cpp:793)
==5272==
==5272== Conditional jump or move depends on uninitialised value(s)
==5272== at 0x4C30BC8: strlen (vg_replace_strmem.c:454)
==5272== by 0x50FAAD0: vfprintf (vfprintf.c:1637)
==5272== by 0x5123488: vsnprintf (vsnprintf.c:114)
==5272== by 0x51017B1: snprintf (snprintf.c:33)
==5272== by 0x400C9E: main (111.c:32)
==5272== Uninitialised value was created by a stack allocation
==5272== at 0x4E860B0: zmq::stream_engine_t::mechanism_ready() (stream_engine.cpp:793)
==5272==
Identifier: de.logicway.pferdepflug
Message: testfahrt
==5272==
==5272== HEAP SUMMARY:
==5272== in use at exit: 0 bytes in 0 blocks
==5272== total heap usage: 210 allocs, 210 frees, 271,368 bytes allocated
==5272==
==5272== All heap blocks were freed -- no leaks are possible
==5272==
==5272== For counts of detected and suppressed errors, rerun with: -v
==5272== ERROR SUMMARY: 6 errors from 3 contexts (suppressed: 0 from 0)
未初始化的值来自stream_engine.cpp:793
,因此可能会向ZeroMQ bug tracker报告。