On 7/4/07, Dan Kennedy <[EMAIL PROTECTED]> 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.
> >
> > Igor Tandetnik
>
> 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).

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

Dan.

> *ppStmt is left pointing to a compiled SQL statement structure that can
> be executed using sqlite3_step(). Or if there is an error, *ppStmt may
> be set to NULL. If the input text contained no SQL (if the input is and
> empty string or a comment) then *ppStmt is set to NULL. The calling
> procedure is responsible for deleting the compiled SQL statement using
> sqlite3_finalize() after it has finished with it.
>
> I also thought this means the statement has to be deleted with
> sqlite3_finalize, even when sqlite3_prepare_v2 failed (except for the
> case where NULL is returned). Notice the "may be set to NULL" in the
> documentation. Doesn't a non-NULL value indicate some memory was
> allocated and thus need to be freed?

It's that "may" indeed that caused the confusion and that is still
present on the current documentation. I suggest perhaps a review of
that paragraph now that it is clear the memory will always be
released.

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

Reply via email to