1

我刚开始使用 Valgrind 检查 C++ 应用程序的内存泄漏。但是每次程序尝试打开 Sqlite3 数据库时,它都会以 SIGSEGV 终止。在没有 Valgrind 的情况下运行时不会发生这种情况。

最简单的演示程序如下

int main (int argc, char** argv) {
    sqlite3 *db = 0;
    sqlite3_open("/tmp/mydb.sqlite", &db);
    return 0;
}

Valgrind 报告:

==29227== Jump to the invalid address stated on the next line
==29227==    at 0x0: ???
==29227==    by 0x48C606: mallocWithAlarm (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x477F71: sqlite3Malloc (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x4BD7F5: pthreadMutexAlloc (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x47740F: sqlite3_initialize (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x48929D: openDatabase (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x10014B507: main (in /usr/local/bin/outbound)
==29227==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==29227== 
==29227== 
==29227== Process terminating with default action of signal 11 (SIGSEGV)
==29227==  Bad permissions for mapped region at address 0x0
==29227==    at 0x0: ???
==29227==    by 0x48C606: mallocWithAlarm (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x477F71: sqlite3Malloc (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x4BD7F5: pthreadMutexAlloc (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x47740F: sqlite3_initialize (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x48929D: openDatabase (in /opt/local/lib/libsqlite3.0.8.6.dylib)
==29227==    by 0x10014B507: main (in /usr/local/bin/outbound)

什么可能导致 Valgrind 出现这种行为?

4

1 回答 1

0

尝试使用传入 sqlite3 的单个 HEAP 对象。

sqlite3_config (SQLITE_CONFIG_HEAP, malloc (1024*1024), 1024*1024, 64);

这会从 valgrind 满意的单个“malloc”分配 1 MB 数据,最小分配大小为 64 字节。

于 2012-07-15T17:42:19.337 回答