c++ - 在 C++ 中使用 sqlite3 时内存泄漏

标签 c++ string sqlite memory-leaks stringstream

程序写入SQLite数据库,通过无线模块接收消息。但是不知何故,每次收到消息并将其写入数据库时​​都会发生内存泄漏,在大约 10 000 次写入后,程序使用了 1GB 的内存。

SQLite3 with C++ 的文档说使用存在的 sqlite3_finalize()sqlite3_close() 可以防止内存泄漏:

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

using namespace std;

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

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

struct messageStruct_t {
  float value;
};

bool isOpenDB = false;

int main() {
  int counter = 0;
  while (1) {
    int header = 1;
    int message = rand() % 3;

    if (message) {
      counter ++;

      switch (header) {

      case 1: {
        messageStruct_t recMessage;
        recMessage.value = 55;
        int receivedSendersID = 2;

        //SQL query to get foreign key
        stringstream strm_select;
        strm_select << "SELECT id FROM table1 WHERE sendersID="
                    << receivedSendersID;
        string s_select = strm_select.str();
        cout << "SQL query: " << s_select << endl;
        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(id,value,sender_id) values("
                      << counter << ", "
                      << recMessage.value << ", " << sendersID << ")";
          string s_insert = strm_insert.str();
          cout << "SQL query: " << s_insert << endl;
          insOrUpdate(s_insert);
          cout << "Recorded data: " << recMessage.value << endl;
        }
      }

      default: {
        break;
      }

      }
    }
  }
}

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;
}

CREATE TABLE table1 (
id INTEGER NOT NULL,
sendersID INTEGER,
PRIMARY KEY (id)
);
CREATE TABLE table2 (
id INTEGER NOT NULL,
value FLOAT,
sender_id INTEGER,
FOREIGN KEY(sender_id) REFERENCES table1 (id)
);

INSERT INTO table1(sendersID) values(2);

最佳答案

在您的 connectDB(..) 调用中,您不会在再次打开数据库之前检查它是否已经打开。您的内存泄漏可能是由于此数据库到您的内存空间的重复映射。

这个程序可能还有其他问题,但下面对 connectDB(..) 的更改应该有助于解决每次成功插入时的泄漏问题。

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

关于c++ - 在 C++ 中使用 sqlite3 时内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35876506/

相关文章:

c# - 如何使用 C# 改进此代码

c# - 错误 CS0012 : The type 'DbConnection' is defined in an assembly that is not referenced

android - 获取表的最大 ID 行

c++ - glReadPixel(一次通过与通过点循环)

c++ - VS 2010 中的 CPP 单元构建错误

c++ - 相机参数已知时的 OpenCV 图像拼接

c++ - 在 CSV 文件中查找相同的名称。 C++

python - 在 C 中解析一个字符串并将其保存到一个结构数组中

java - 如何在不使用 split 和 stringtokenizer 的情况下反转 java 字符串中的单词

database - 获取 “java.sql.SQLException: Values not bound to statement” 异常