我编写了2个简单函数。
第一个与我的数据库建立连接并启用外键支持:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="nv.1.8.1.d3.css">
第二个关闭连接:
int hmd_db_connect (sqlite3 *db) {
char *sql;
sqlite3_stmt *stmt;
if (sqlite3_open_v2(HMD_DB_PATH, &db,
SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_SHAREDCACHE,
NULL) != SQLITE_OK) {
hmd_log_err("Failed to connect to database [Path: %s]: %s.",
HMD_DB_PATH, sqlite3_errmsg(db));
return -1;
}
sql = hmd_make_str("PRAGMA foreign_keys = ON");
if (sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL) != SQLITE_OK) {
hmd_log_err("Failed to prepare SQL [%s]: %s.",
sql, sqlite3_errmsg(db));
hmd_db_disconnect(db);
free(sql);
return -1;
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
hmd_log_err("Failed to enable foreign key support: %s.",
sqlite3_errmsg(db));
sqlite3_finalize(stmt);
hmd_db_disconnect(db);
free(sql);
return -1;
}
sqlite3_finalize(stmt);
free(sql);
return 0;
}
然后在我的int hmd_db_disconnect (sqlite3 *db) {
if (sqlite3_close(db) != SQLITE_OK) {
hmd_log_err("Failed to disconnect from database [Path: %s]: %s.",
HMD_DB_PATH, sqlite3_errmsg(db));
return -1;
}
return 0;
}
函数中,我将这两个函数称为:
main
程序没有显示任何错误消息。但是当我用_info("Connecting to database.\n");
hmd_db_connect(db);
_info("Database connected.\n");
_info("Disconnecting from database.\n");
hmd_db_disconnect(db);
_info("Database disconnected.\n");
运行它时,它显示可能的内存泄漏:
valgrind
我已经检查了==11079== LEAK SUMMARY:
==11079== definitely lost: 0 bytes in 0 blocks
==11079== indirectly lost: 0 bytes in 0 blocks
==11079== possibly lost: 57,768 bytes in 47 blocks
==11079== still reachable: 0 bytes in 0 blocks
==11079== suppressed: 0 bytes in 0 blocks
==11079==
==11079== For counts of detected and suppressed errors, rerun with: -v
==11079== ERROR SUMMARY: 47 errors from 47 contexts (suppressed: 6 from 6)
的日志,在sqlite3的共享库中检测到所有错误,并且我没有看到任何不自由的变量。
请问好吗?
答案 0 :(得分:2)
我认为sqlite3*
不正确。它应该返回sqlite3*
,但需要sqlite3**
而不是sqlite3*
。您只需丢失sqlite3_open_v2
返回的sqlite3* db = NULL;
hmd_db_connect(db);
,想象一下:
db
即使hmd_db_connect
成功打开数据库, int hmd_db_connect(sqlite3 **db) {
char *sql;
sqlite3_stmt *stmt;
if (sqlite3_open_v2(HMD_DB_PATH, db,
SQLITE_OPEN_FULLMUTEX | SQLITE_OPEN_SHAREDCACHE,
NULL) != SQLITE_OK) {
hmd_log_err("Failed to connect to database [Path: %s]: %s.",
HMD_DB_PATH, sqlite3_errmsg(*db));
return -1;
}
sql = hmd_make_str("PRAGMA foreign_keys = ON");
if (sqlite3_prepare_v2(*db, sql, strlen(sql), &stmt, NULL) != SQLITE_OK) {
hmd_log_err("Failed to prepare SQL [%s]: %s.",
sql, sqlite3_errmsg(*db));
hmd_db_disconnect(*db);
free(sql);
return -1;
}
if (sqlite3_step(stmt) != SQLITE_DONE) {
hmd_log_err("Failed to enable foreign key support: %s.",
sqlite3_errmsg(*db));
sqlite3_finalize(stmt);
hmd_db_disconnect(*db);
free(sql);
return -1;
}
sqlite3_finalize(stmt);
free(sql);
return 0;
}
仍然为NULL。
我修改了代码:
<a onClick="sessionStorage.someKey = 'category 1'; console.log(sessionStorage.someKey);" data-fajax="false" href="some-page.html" data-transition="slidefade">Category 1</a>
现在似乎没有泄漏。