我不确定这里发生了什么,但我发现在finalize / close sqlite阶段,sqlite3_column_text返回的数据正在被更改.
// rc not handled in this abbreviated code sqlite3 *db; sqlite3_stmt *stmt; char * sql; const char * tail; int rc; char * dbName = "C:\\db\\myblobs.db"; int myIndex = 0; char * myLocation1; string myLocation2; rc = sqlite3_open(dbName,&db); sql = "SELECT location FROM blobs WHERE key = ?"; rc = sqlite3_prepare(db,sql,strlen(sql),&stmt,&tail); sqlite3_bind_int(stmt,1,myIndex); rc = sqlite3_step(stmt); myLocation1 = (char*)sqlite3_column_text(stmt,0); myLocation2 = (char*)sqlite3_column_text(stmt,0); // can process myLocation1 & myLocation2 fine here sqlite3_finalize(stmt); // data myLocation1 points to get corrupted sqlite3_close(db); // data myLocation2 points to gets further corrupted
问题与myLocation1有关.它指向的数据很好,直到它到达sqlite3_finalize和sqlite3_close语句.然而,mylocation2保持不变.所以不确定这里发生了什么.执行sqlite3_close(db)后,myLocation1在Visual Studio 2010中被标识为“Bad Ptr”.
任何帮助最受赞赏.
从
fine manual:
原文链接:https://www.f2er.com/sqlite/238913.html
const unsigned char *sqlite3_column_text(sqlite3_stmt*,int iCol);
[…]
The pointers returned are valid until a type conversion occurs as described above,or untilsqlite3_step()
orsqlite3_reset()
orsqlite3_finalize()
is called.
因此,只要调用sqlite3_finalize,sqlite3_column_text的返回值就会变为无效,而myLocation1和myLocation2指针指向垃圾.
如果在调用sqlite3_finalize之后需要这些字符串,则必须将它们复制到您控制的内存中.实际上,由于在类型转换发生之前有效,因此应立即复制它们并仅使用(char *)sqlite3_column_text(stmt,0);而你正在制作自己的副本.