1

我正在尝试使用 C-API 连接到 mongodb,我的代码似乎工作正常。然而,仔细检查 Valgrind 抱怨说我在做非法的事情。

我的程序接受参数-h <hostname>,然后将此字符串转换为 amongodb_uri然后尝试连接到 mongodb:

*client_p = mongoc_client_new(host);

if (!*client_p) {
   log_die("Failed to parse URI!");
} 

我的程序按预期运行,但是当我用 valgrind 检查它时,调用会mongoc_client_new导致 SIGSEGV:

==28775== Memcheck, a memory error detector
==28775== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==28775== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==28775== Command: ./coll2tde -h mongodb://localhost -d test -c testq
==28775== 
aTrying to connect to mongodb://localhost
2014/12/25 22:22:21.0255: [28775]:    DEBUG:      cluster: Client initialized in direct mode.
==28775== Jump to the invalid address stated on the next line
==28775==    at 0xFFFFFFFFFF600800: ???
==28775==    by 0x402722: get_cursor (mongo.c:26)
==28775==    by 0x402330: main (coll2tde.c:120)
==28775==  Address 0xffffffffff600800 is not stack'd, malloc'd or (recently) free'd
==28775== 
==28775== 
==28775== Process terminating with default action of signal 11 (SIGSEGV)
==28775==  Bad permissions for mapped region at address 0xFFFFFFFFFF600800
==28775==    at 0xFFFFFFFFFF600800: ???
==28775==    by 0x402722: get_cursor (mongo.c:26)
==28775==    by 0x402330: main (coll2tde.c:120)
==28775== 
==28775== HEAP SUMMARY:
==28775==     in use at exit: 114,751 bytes in 3,194 blocks
==28775==   total heap usage: 3,615 allocs, 421 frees, 177,386 bytes allocated
==28775== 
==28775== LEAK SUMMARY:
==28775==    definitely lost: 6,784 bytes in 1 blocks
==28775==    indirectly lost: 2,968 bytes in 11 blocks
==28775==      possibly lost: 1,462 bytes in 19 blocks
==28775==    still reachable: 103,537 bytes in 3,163 blocks
==28775==         suppressed: 0 bytes in 0 blocks
==28775== Rerun with --leak-check=full to see details of leaked memory
==28775== 
==28775== For counts of detected and suppressed errors, rerun with: -v
==28775== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 8 from 6)
Killed

一开始我认为这是我的代码有问题,但后来我从mogoc_client文档中编译了示例并看到了相同的行为。我想知道这是否是一个错误或者可以安全地忽略它。

4

1 回答 1

1

在花了几个小时试图解决这个问题之后,事实证明这是旧版本的 Valgrind(Debian Wheezy 上的 3.7)的问题。我从上游来源编译了 3.10.1 版本的 Valgrind,这个问题就消失了。

于 2014-12-25T21:50:03.667 回答