Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What if sqlite3_open() returns an error AND a valid database connection?

Tags:

sqlite

The documentation for sqlite3_open() says this:

"A database connection handle is usually returned in *ppDb, even if an error occurs."

Does this mean that if sqlite3_open() returns something other than SQLITE_OK AND a non null database handle that you owe a sqlite3_close() before attempting sqlite3_open() again?

If so, this could explain a problem I'm having where randomly a database fails to open because it is locked.

like image 858
dicroce Avatar asked Apr 13 '26 17:04

dicroce


1 Answers

Yes, you need to close the sqlite3 connection object if it's returned as non null.

The sqlite3_open() manual is quite clear;

Whether or not an error occurs when it is opened, resources associated with the database connection handle should be released by passing it to sqlite3_close() when it is no longer required.

At the very least, you'll have a memory leak of the sqlite3 connection object itself if you don't, since sqlite3_close() frees that object and any resources allocated to it.

like image 130
Joachim Isaksson Avatar answered Apr 15 '26 08:04

Joachim Isaksson