You are preparing statements inside the loop, but only finalizing the last one 
(i.e. outside the loop)

-----Urspr√ľngliche Nachricht-----
Von: sqlite-users [] Im 
Auftrag von Lev
Gesendet: Sonntag, 05. November 2017 01:28
Betreff: [EXTERNAL] [sqlite] memory leak

I'm fighting with some memory leak. From time to time the vsize of the process 
goes up with 100k. I have several function like this, and I call them in each 
second. My memory growth happens every 10 minutes or so.

int dbSqliteSelectSensorsToReport(sdmd_t *sdmd, sensor_t *sensor, int 
deviceType, int target) { char sqlQuery[256];
        char *tableName;
        char *targetName;
        int haveWork = 1;
        int ret, retVal;
        sqlite3_stmt *sq3Stmt;

        /*Reset the state whatever it is*/

        sensor->stat = SENSOR_DATA_INVALID;

        do {

                switch (deviceType) {
                        case SENSOR_TYPE_AN:
                        tableName = "analog";
                        case SENSOR_TYPE_DIG:
                        tableName = "digital";
                        case SENSOR_TYPE_UNKNOWN:
                        tableName = NULL;

                switch (target) {
                        case TARGET_MQTT:
                        targetName = "read_mqtt";
                        case TARGET_MODBUS: /*This makes no sence,
                hence it is done in an other process... but whatever*/
                targetName = NULL; break;
                        targetName = NULL;

                if (targetName == NULL || tableName == NULL) {
                        retVal = DB_SQLITE_ERR;

                sprintf(sqlQuery, "SELECT rowid, sensor_id, value, time
                FROM %s WHERE %s=0 LIMIT 1;", tableName,
                targetName); /*Read one by one. I don't want to block
                DB access.*/

                sqlite3_prepare_v2(sdmd->db, sqlQuery, -1, &sq3Stmt,

                while (haveWork == 1) {
                        ret = sqlite3_step(sq3Stmt);
                        switch (ret) {
                                case SQLITE_ROW:
                                /*Read the data*/
                                sensor->id =
                sqlite3_column_int64(sq3Stmt, 0); sensor->sensorId =
                sqlite3_column_int64(sq3Stmt, 1); sensor->value =
                sqlite3_column_double(sq3Stmt, 2); sensor->time =
                sqlite3_column_int64(sq3Stmt, 3); sensor->stat =
                SENSOR_DATA_VALID; sprintf(sensor->name, "%s_%d",
                tableName, sensor->sensorId); break;
                                case SQLITE_DONE:
                                haveWork = 0;
                                retVal = DB_SQLITE_OK;
                                case SQLITE_BUSY:
                                debug(sdmd->dbg, DEBUG_ERR, "%s():
                sqlite (BUSY): %s\n", __func__,
                sqlite3_errmsg(sdmd->db)); haveWork = 0; retVal =
                DB_SQLITE_ERR; break;
                                haveWork = 0;
                                debug(sdmd->dbg, DEBUG_ERR, "%s():
                sqlite (%d): %s\n", __func__, ret,
                sqlite3_errmsg(sdmd->db)); debug(sdmd->dbg, DEBUG_ERR,
                "%s(): sqlite query: %s\n", __func__, sqlQuery); retVal
                = DB_SQLITE_ERR; break; }
        } while(0);

        sqlite3_finalize(sq3Stmt); /*Release the dB*/

        return retVal;

What do I do wrong?

Any help is appreciated.
sqlite-users mailing list

 Gunter Hick | Software Engineer | Scientific Games International GmbH | 
Klitschgasse 2-4, A-1130 Vienna | FN 157284 a, HG Wien, DVR: 0430013 | (O) +43 
1 80100 - 0

May be privileged. May be confidential. Please delete if not the addressee.
sqlite-users mailing list

Reply via email to