Hi,

In my C++ application I'm writing/reading some blob data to the
database. This is sample of my code (in 99% taken from example i found
in the internet ):

/**
 * This function reads one blob value asqued in zSql query into memory
pointed by pzBlob
 * and returns size of allocated memory in pnBlob integer. One MUST
remember to deallocate
 * memory provided by pzBlob pointer.
 * Example: readBlob("SELECT value FROM table WHERE id=5", &test1, &lenTest);
 * \param *zSql Query string f.e. "SELECT value FROM table WHERE id=5"
 * \param **pzBlob Pointer to memory where data is retuned. This
memory is allocated automatically. One MUST remember to deallocate it.
 * \param *pnBlob Size of data returned in pzBlob
 * \return False if anything went wrong, else True;
 */
bool SQLite3pp::readBlob(const char *zSql, unsigned char **pzBlob, int *pnBlob){
  if(zErrMsg) sqlite3_free(zErrMsg);

  sqlite3_stmt *pStmt;
  int rc;

  /* In case there is no table entry for key zKey or an error occurs,
  ** set *pzBlob and *pnBlob to 0 now.
  */
  *pzBlob = 0;
  *pnBlob = 0;

  bool retValue = false;

  do {
    /* Compile the SELECT statement into a virtual machine. */
    rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
             // Line 164
    if( rc!=SQLITE_OK ){
      return false;
    }
        
        

    /* Run the virtual machine. We can tell by the SQL statement that
    ** at most 1 row will be returned. So call sqlite3_step() once
    ** only. Normally, we would keep calling sqlite3_step until it
    ** returned something other than SQLITE_ROW.
    */
    rc = sqlite3_step(pStmt);
    if( rc==SQLITE_Row ){
      /* The pointer returned by sqlite3_column_blob() points to memory
      ** that is owned by the statement handle (pStmt). It is only good
      ** until the next call to an sqlite3_XXX() function (e.g. the
      ** sqlite3_finalize() below) that involves the statement handle.
      ** So we need to make a copy of the blob into memory obtained from
      ** malloc() to return to the caller.
      */
      *pnBlob = sqlite3_column_bytes(pStmt, 0);
      *pzBlob = (unsigned char *)malloc(*pnBlob);
      memcpy(*pzBlob, sqlite3_column_blob(pStmt, 0), *pnBlob);

          retValue=true;
    }

    /* Finalize the statement (this releases resources allocated by
    ** sqlite3_prepare() ).
    */
    rc = sqlite3_finalize(pStmt);

    /* If sqlite3_finalize() returned SQLITE_SCHEMA, then try to execute
    ** the statement all over again.
    */
  } while( rc==SQLITE_SCHEMA );

  return retValue;
}


As you can see I use sqlite3_prepare/sqlite3_finalize pairs. My memory
leak checker ( Deleaker : www.deleaker.com ) reports memory leak at
sqlite3_prepare :

msvcrt.dll!malloc
sqlite3.dll!sqlite3_malloc + 120 bytes
sqlite3.dll!sqlite3_realloc + 27 bytes
sqlite3.dll!sqlite3_release_memory + 200 bytes
sqlite3.dll!sqlite3_release_memory + 242 bytes
sqlite3.dll!sqlite3_set_authorizer + 1072 bytes
sqlite3.dll!sqlite3_set_authorizer + 22149 bytes
sqlite3.dll!sqlite3_declare_vtab + 11632 bytes
sqlite3.dll!sqlite3_prepare16_v2 + 18070 bytes
sqlite3.dll!sqlite3_declare_vtab + 27000 bytes
sqlite3.dll!sqlite3_declare_vtab + 31896 bytes
sqlite3.dll!sqlite3_declare_vtab + 36104 bytes
sqlite3.dll!sqlite3_reset_auto_extension + 12492 bytes
sqlite3.dll!sqlite3_reset_auto_extension + 13270 bytes
sqlite3.dll!sqlite3_prepare + 31 bytes
RSR2.exe!SQLite3pp::readBlob Line 164
(c:\furtado\furtado\sqlite\sqlite3pp.cpp)

I am closing database properly.
Am I doing something wrong or is it some kind of bug?


Bartosz Wiklak
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to