加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在finalize / close期间,sqlite3_column_text返回的数据被破坏

发布时间:2020-12-12 23:40:42 所属栏目:百科 来源:网络整理
导读:我不确定这里发生了什么,但我发现在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:d
我不确定这里发生了什么,但我发现在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:dbmyblobs.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:

const unsigned char *sqlite3_column_text(sqlite3_stmt*,int iCol);
[…]
The pointers returned are valid until a type conversion occurs as described above,or until sqlite3_step() or sqlite3_reset() or sqlite3_finalize() is called.

因此,只要调用sqlite3_finalize,sqlite3_column_text的返回值就会变为无效,而myLocation1和myLocation2指针指向垃圾.

如果在调用sqlite3_finalize之后需要这些字符串,则必须将它们复制到您控制的内存中.实际上,由于在类型转换发生之前有效,因此应立即复制它们并仅使用(char *)sqlite3_column_text(stmt,0);而你正在制作自己的副本.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读