#1 I don't see where you're freeing m_szErrorString (not real sure if it gets 
malloc'd on success) -- but you do need to free it on errors for sure.



And where's your Callback function?  Why are you calling SaveResultSet (which 
you also don't show)?  That should probably be done inside the Callback because 
I believe the data is destroyed coming out of the Callback.





Michael D. Black

Senior Scientist

NG Information Systems

Advanced Analytics Directorate



________________________________
From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on 
behalf of Ian Hardingham [i...@omroth.com]
Sent: Tuesday, May 10, 2011 7:17 AM
To: General Discussion of SQLite Database
Subject: EXT :[sqlite] Memory leak in SQlite

Hey guys.

I'm sure that this is to do with the way I am using SQLite. I do not
have time to radically change my methodology at this point, but I do
need to fix a rather severe memory leak I'm having.

(My apologies for the code)

Any help is much appreciated.

I query like this:


int SQLiteObject::ExecuteSQL(const char* sql, int hack)
{
    int iResult;
    sqlite_resultset* pResultSet;

    // create a new resultset
    pResultSet = new sqlite_resultset;

    if (pResultSet)
    {
       pResultSet->bValid = false;
       pResultSet->iCurrentColumn = 0;
       pResultSet->iCurrentRow = 0;
       pResultSet->iNumCols = 0;
       pResultSet->iNumRows = 0;
       pResultSet->iResultSet = m_iNextResultSet;
       pResultSet->vRows.clear();
       m_iLastResultSet = m_iNextResultSet;
       m_iNextResultSet++;
    }
    else
       return 0;


     iResult = sqlite3_exec(m_pDatabase, sql, Callback,
(void*)pResultSet, &m_szErrorString);

     if (iResult == 0)
     {
         //SQLITE_OK


         SaveResultSet(pResultSet);
         //Con::executef(this, 1, "onQueryFinished()");
         return pResultSet->iResultSet;

     }
     else
     {
         // error occured
         Con::executef(this, 2, "onQueryFailed", m_szErrorString);
         delete pResultSet;
         return 0;
     }

    return 0;
}


And I "clear a result" like this:


void SQLiteObject::ClearResultSet(int index)
{



sqlite_resultset* resultSet;
    sqlite_resultrow* resultRow;
    S32 rows, cols, iResultSet;

    // Get the result set specified by index
    resultSet = GetResultSet(index);
    iResultSet = GetResultSetIndex(index);
    if ((!resultSet) || (!resultSet->bValid))
    {
       Con::warnf("Warning SQLiteObject::ClearResultSet(%i) failed to
retrieve specified result set.  Result set was NOT cleared.", index);
       return;
    }
    // Now we have the specific result set to be cleared.
    // What we need to do now is iterate through each "Column" in each "Row"
    // and free the strings, then delete the entries.
    VectorPtr<sqlite_resultrow*>::iterator iRow;
    VectorPtr<char*>::iterator iColumnName;
    VectorPtr<char*>::iterator iColumnValue;

    for (iRow = resultSet->vRows.begin(); iRow !=
resultSet->vRows.end(); iRow++)
    {
       // Iterate through rows
       // for each row iterate through all the column values and names
       for (iColumnName = (*iRow)->vColumnNames.begin(); iColumnName !=
(*iRow)->vColumnNames.end(); iColumnName++)
       {
          // Iterate through column names.  Free the memory.
          delete[] (*iColumnName);
       }
       for (iColumnValue = (*iRow)->vColumnValues.begin(); iColumnValue
!= (*iRow)->vColumnValues.end(); iColumnValue++)
       {
          // Iterate through column values.  Free the memory.
          delete[] (*iColumnValue);
       }
       // free memory used by the row
       delete (*iRow);
    }
    // empty the resultset
    resultSet->vRows.clear();
    resultSet->bValid = false;
    delete resultSet;
    m_vResultSets.erase_fast(iResultSet);
}

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to