Hi Jens,
silverstorm82 wrote:The problem in your first code version is that the ownership of the internal SQLite statement is passed on to the newly created wxSQLite3Statement instance. On executing delete for this instance the internal SQLite statement is finalized thus the SQLite statement pointer in your statement1 resp. statement2 instance isn't valid any longer.
I don't understand why the original statement1 resp. statement2 are not valid any longer because I created, used and deleted a copy of the statements (pStmt = new wxSQLite3Statement(statement1)). Do you have any explanation for that? I'd like to understand that.
Ok, so here comes the whole story in greater detail:
A prepared SQL statement is represented by an opaque data structure by SQLite and SQLite returns a pointer to this data structure. wxSQLite3Statement is just a wrapper class for this statement pointer. The problem is that SQLite requires that a statement is finalized after use otherwise you get in trouble, i.e. you'll experience deadlocks or other problems. But to make matters complicated a statement can be finalized only once. So I introduced the concept of "ownership" into class wxSQLite3Statement. Only one instance can hold the ownership of the associated statement pointer and is responsible for finalizing the statement. If you use the copy constructor as in your code (or the assignment operator) the ownership is forwarded to the wxSQLite3Statement target instance. The source instance looses the ownership (in your code: statement1 and statement2 loose ownership). Nevertheless the statement pointer is valid for both instances as long as it was not finalized. But in your code the statement is implicitly finalized on executing
delete pStmt.
As soon as a wxSQLite3Statement instance goes out of scope or is deleted, it's destructor is called and looks at the ownership indicator. If the instance owns the statement pointer it finalizes the statement. Thereafter no other wxSQLite3Statement instance holding a copy of the statement pointer can access the statement. If you do the application will crash.
Unfortunately there is no way to detect whether a statement pointer is still valid or not. Sure one could keep track of all instances holding statement pointers, but keep in mind that wxSQLite3 is a
thin wrapper around SQLite. In most scenarios you don't pass around wxSQLite3Statement instances a lot and there is little gain from keeping track because you would always have to check whether the statement was finalized or not. Therefore I decided to not add this feature to wxSQLite3.
Regards,
Ulrich