On Tue, May 15, 2012 at 08:49:50PM +0100, Simon Slavin scratched on the wall:
> 
> On 15 May 2012, at 8:38pm, Baruch Burstein <bmburst...@gmail.com> wrote:
> 
> >  I am working on a  C++ wrapper for sqlite. It
> > is the wrapper's user's responsibility to make sure no statement objects
> > still exist before the database object gets destroyed. This is just a
> > precaution.

> I am surprised that doing _close() without finalizing doesn't do the
> finalizing before (or instead of) generating the error code.  After
> all, one is unlikely to call _close() in error, and slowing down
> _close() isn't going to slow down most apps very much.

  That's missing the point.  You're never supposed to get there.
  Having unaccounted for statements when you close the database is
  essentially a memory leak.  You've got data structures the
  application lost track of and didn't clean up.

  Yes, you can loop over the current statements and finalize them, but
  this also frees the memory used by those statements, making it invalid.
  That means one of two things MUST be true:

  1) Your application lost track of statements.  This is a
     memory/resource leak.  You should fix the problem, not the
     consequences.
 
  2) After the call to _close(), your application now has a non-NULL
     pointer somewhere that it THINKS points to valid statement, but in
     reality points to a random chunk of memory.  Attempting to do
     *anything* with this pointer-- including another call to
     _finalize()-- will likely crash the program.

  About the only time it is valid to loop over statements and finalize
  them is when an application is about to exit.  Even then, you risk
  issues, especially if you've got a swarm of C++ objects with
  distructors.  You would likely be safer to call exit and just let the
  OS dump everything.  You might still risk a crash, however.

> I'm also surprised at how strong the warning is on the page about
> sqlite3_finalize():
> 
> <http://sqlite.org/c3ref/finalize.html>
> 
> This might be the strongest warning in the whole of the API functionu
> documentation.  Couldn't _finalize() just put a null somewhere in the
> statement as a flag that the statement had already been finalized ?
> I guess checking for it would slow up later calls too much.

  _finalize() frees the statement data structure.  There is no place to
  flag or mark the statement as invalid.  All you have is a bunch of
  pointers that point to junk...  Just like the second loop iteration
  of the first choice from the OP's post.


   -j

-- 
Jay A. Kreibich < J A Y  @  K R E I B I.C H >

"Intelligence is like underwear: it is important that you have it,
 but showing it to the wrong people has the tendency to make them
 feel uncomfortable." -- Angela Johnson
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to