On Tue, May 15, 2012 at 10:38:29PM +0300, Baruch Burstein scratched on the wall: > On Tue, May 15, 2012 at 10:34 PM, Jay A. Kreibich <j...@kreibi.ch> wrote: > > > On Tue, May 15, 2012 at 10:17:41PM +0300, Baruch Burstein scratched on > > the wall: > > > Which of the following should I be running right before calling > > > sqlite3_close? > > > > > > while(stmt = sqlite3_next_stmt(db, stmt)) > > > sqlite3_finalize(stmt); > > > > > > while(stmt = sqlite3_next_stmt(db, NULL)) > > > sqlite3_finalize(stmt); > > > > > > I am not sure which will have the desired effect. > > > > The second, since "stmt" will be an invalid pointer by the time > > the loop comes around and hits sqlite3_next_stmt() again. > > > > Ideally, however, your application should manage its own statements > > correctly. This type of brute-force clean-up can act as a safety net, > > but it can also leave dangling pointers elsewhere in the code. > > Something in your code must know about those statements... and if > > not, then you're leaking memory, which is just as bad. > > > > I am aware of this. However, 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.
If the statement object has a destructor that finalizes the SQLite statement, it is very likely to cause an bus error when it tries to clean up those objects. To really do this right, the database object needs to know about all the staement objects associated with it, or the database object needs some way to find a statement object, given a database pointer and a stmt pointer (for example, a class-static hash of all instanced statement objects). When doing something like this you wouldn't need to (or want to) destroy the statement objects, but you'd at least want to NULL out the stmt pointer inside the statement object so the object doesn't attempt to double finalize it, and the object itself knows it is no longer valid. -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