事情是这样:
今天测试视觉建图相关信息存入sqlite3 db中,在存入第二条数据时,提示database is locked。
因为存在multi threading操作db的情况,最开始以为是diff threading同时操作引起的,后来发现不是,因为multithreading也是顺序执行,不会并行。最后发现是有一个接口里在处理sqlite3_stmt *stmt后,中间有return的分支,没有调用sqlite3_finalize(stmt),造成locked。
另外,sqlite3_prepare_v2如果失败是不需要调用sqlite3_finalize的,下面是sqlite3的注释,sqlite3_finalize释放了NULL也没什么问题。
/*
** CAPI3REF: Destroy A Prepared Statement Object
** DESTRUCTOR: sqlite3_stmt
**
** ^The sqlite3_finalize() function is called to delete a [prepared statement].
** ^If the most recent evaluation of the statement encountered no errors
** or if the statement is never been evaluated, then sqlite3_finalize() returns
** SQLITE_OK. ^If the most recent evaluation of statement S failed, then
** sqlite3_finalize(S) returns the appropriate [error code] or
** [extended error code].
**
** ^The sqlite3_finalize(S) routine can be called at any point during
** the life cycle of [prepared statement] S:
** before statement S is ever evaluated, after
** one or more calls to [sqlite3_reset()], or after any call
** to [sqlite3_step()] regardless of whether or not the statement has
** completed execution.
**
** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
**
** The application must finalize every [prepared statement] in order to avoid
** resource leaks. It is a grievous error for the application to try to use
** a prepared statement after it has been finalized. Any use of a prepared
** statement after it has been finalized can result in undefined and
** undesirable behavior such as segfaults and heap corruption.
Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.