Dan Kennedy wrote:
On Wed, 2007-07-04 at 09:58 +0200, Jef Driesen wrote:
Igor Tandetnik wrote:
Mario Figueiredo wrote:
I'm a tad bit confused with sqlite3_finalize() usage when the query
fails. As it is, I'm assuming it releases resources and I use it in
this context:

--------------------------------
rc = sqlite3_prepare_v2(/* ... */);
if (rc != SQLITE_OK)
{
  sqlite3_finalize(stmt);
  /* ... */
}
This doesn't make any sense. If prepare fails, you do not have a valid statement handle to call finalize on.

Are you sure about that? The documentation for sqlite3_prepare_v2 says:

Igor is, as usual, correct.

The situation in 3.4.0 is that if sqlite3_prepare() returns other than SQLITE_OK, you are guaranteed that *ppStmt is set to NULL.
You may call sqlite3_finalize() on this if you wish - it's a no-op.

Historically, it may have been that *ppStmt was sometimes left
uninitialized if an error occured (hence the "may" in the docs).

What do you mean with uninitialized? Leaving the pointer unchanged, or pointing to some memory that is already freed or still needs to be freed? This is important if you need to support older versions.

I suppose you mean the first one, but I'm asking anyway just to be sure. In my code, I always initialize pointers to NULL, so this case would not cause any problems at all. The second case is a completely different story of course.

Anyway, the code I'm using should be fine in all cases:

   sqlite3_stmt *stmt = 0;
#if SQLITE_VERSION_NUMBER >= 3003009
   int rc = sqlite3_prepare_v2 (db, sql, nbytes, &stmt, tail);
#else
   int rc = sqlite3_prepare (db, sql, nbytes, &stmt, tail);
#endif
   if (rc != SQLITE_OK && stmt != 0) {
      sqlite3_finalize (stmt);
      stmt = 0;
   }

This is in contrast to sqlite3_open(). You must call sqlite3_close(),
even if sqlite3_open() returned an error code.

I know. I had a topic on that a few months ago [1].

[1] http://www.mail-archive.com/sqlite-users@sqlite.org/msg21324.html



-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to