mardi 8 mars 2016

Memory leak when using sqlite3 with C++

The program writes into SQLite database, the messages are received through a wireless module. But somehow there is a memory leak every time a message is received and written to the database, after about 10 000 writes the program is using 1GB of memory.

The documentation for SQLite3 with C++ says that memory leaks are prevented with sqlite3_finalize() and sqlite3_close() which are present:

#include <iostream>
#include <string>
#include <sstream>
#include "sqlite3.h"

using namespace std;

#define DB "app.db"
sqlite3 *dbfile;

bool connectDB();
void disonnectDB();
int insOrUpdate(string s);
int select(string s);

bool isOpenDB = false;

int main() {
  while (1) {
    if (message.available()) {  //comes from library for wireless communication
      headerStruct_t header;

      switch (header.type) {

      case 'D': {
        messageStruct_t recMessage;
        //the message is received here and written to recMessage
        message.read(header, &recMessage, sizeof(recMessage));

        //SQL query to get foreign key
        stringstream strm_select;
        strm_select << "SELECT id FROM table1 WHERE sendersID IS "
                    << message.getSendersID();
        string s_select = strm_select.str();
        int sendersID = select(s_select);
        cout << "Sender's ID: " << sendersID << endl;


        if (sendersID == 0) {
          cout << "Error: Sender doesn't exist\n";
        } else {
          stringstream strm_insert;
          strm_insert << "INSERT into table2(value,sender_id) values("
                      << recMessage.value << ", " << sendersID << ")";
          string s_insert = strm_insert.str();
          insOrUpdate(s_insert);
          cout << "Recorded data: " << recMessage.value << endl;
        }
      }

      }
    }
  }
}

bool connectDB () {
  if (sqlite3_open(DB, &dbfile) == SQLITE_OK) {
    isOpenDB = true;
    return true;
  }
  return false;
}

void disonnectDB () {
  if ( isOpenDB == true ) {
    sqlite3_close(dbfile);
  }
}

int insOrUpdate(string s) {
  if (!connectDB()) {
    return 0;
  }

  char *str = &s[0];
  sqlite3_stmt *statement;
  int result;
  const char *query = str;

  if (sqlite3_prepare(dbfile, query, -1, &statement, 0) == SQLITE_OK) {
    result = sqlite3_step(statement);
    //the documentation says that this destroys the statement and prevents memory leaks
    sqlite3_finalize(statement);
    return result;
  }
  //and this destroys the db object and prevents memory leaks
  disonnectDB();
  return 0;
}

int select(string s) {
  if (!connectDB()) {
    return 0;
  }

  char *str = &s[0];
  sqlite3_stmt *statement;
  const char *query = str;
  string returned;
  if (sqlite3_prepare(dbfile, query, -1, &statement, 0) == SQLITE_OK) {
    int ctotal = sqlite3_column_count(statement);
    int res = 0;

    while (1) {
      res = sqlite3_step(statement);
      if (res == SQLITE_ROW) {
        for (int i = 0; i < ctotal; i++) {
          string s = (char*)sqlite3_column_text(statement, i);
          cout << s << " ";
          returned = s;
        }
        cout << endl;
      }
      if (res == SQLITE_DONE || res == SQLITE_ERROR) {
        cout << "done " << endl;
        break;
      }
    }
  } else {
    cout << "Can't prepare" << endl;
    return 0;
  }
  sqlite3_finalize(statement);
  disonnectDB();

  int result;
  stringstream convert(returned);
  if (!(convert >> result)) {
    result = 0;
  }

  return result;
}

struct messageStruct_t {
  float value;
  uint16_t pin;
  char _padding[2];
};

Aucun commentaire:

Enregistrer un commentaire