Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. please help me with sqlite3 problem.

please help me with sqlite3 problem.

Scheduled Pinned Locked Moved Database
databasehelpsqlitelinuxperformance
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mimi0525
    wrote on last edited by
    #1

    Hi, everyone, I'm using sqlite 3.6.22 in an ARM-linux based application, and I was annoyed a lot by a memory-leak problem. this is the problem: sqlite3_free_table(char **result) doesn't exactly release the memory allocated by sqlite3_get_table function. To verify it I made it called dirrectly after sqlite3_get_table() function, but I can still access the azResult parameter. bellow is part of my code: sqlite3 *db; char *sqlcmd; int nrow ; int ncolumn ; char **azResult; char *zErr; int rc; rc = sqlite3_open("/usr/YY_AEACard.db", &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return -1 ; } sqlcmd = sqlite3_mprintf("select CardID,CardType,BgnDate,EndDate,ValidTimes,CardRight,CardFlag, EnabledPorts,LastAccessPort,CardPsw from tb_CardInfo where CardID=%d and EnabledPorts = %d\n",card->CardID, nPort ); sqlite3_get_table( db, sqlcmd, &azResult, &nrow, &ncolumn, &zErr ); sqlite3_free( sqlcmd ); /////////////////////////////////////////////////////////// sqlite3_free_table(azResult); /////////////////////////////////////////////////////////// // it still works when i do this here. if (rc != SQLITE_OK) { if (zErr != NULL) { fprintf(stderr, "SQL error: %s\n", zErr); sqlite3_free(zErr); } } if(nrow <= 0) { printf("No data from db!\n"); return -1; } if(ncolumn < 9) { printf("Columns are not enough for card table!\n"); return -1; } bzero(card, sizeof(card)); card->CardID = atoi(azResult[ncolumn+0]); card->CardType = atoi(azResult[ncolumn+1]); strcpy(card->BgnDate, azResult[ncolumn+2]); strcpy(card->EndDate, azResult[ncolumn+3]); card->ValidTimes = atoi(azResult[ncolumn+4]); card->CardRight = atoi(azResult[ncolumn+5]); card->CardFlag = atoi(azResult[ncolumn+6]); card->EnabledPorts = atoi(azResult[ncolumn+7]); card->LastAccessPort = atoi(azResult[ncolumn+8]); strcpy(card->CardPsw, azResult[ncolumn+9]); sqlite3_free_table(azResult); //this is a test for the memory leak printf("%s\n", azResult[4]); //funny enough, it still works here. sqlite3_close(db); return 0; please help me, thank you.

    P 1 Reply Last reply
    0
    • M mimi0525

      Hi, everyone, I'm using sqlite 3.6.22 in an ARM-linux based application, and I was annoyed a lot by a memory-leak problem. this is the problem: sqlite3_free_table(char **result) doesn't exactly release the memory allocated by sqlite3_get_table function. To verify it I made it called dirrectly after sqlite3_get_table() function, but I can still access the azResult parameter. bellow is part of my code: sqlite3 *db; char *sqlcmd; int nrow ; int ncolumn ; char **azResult; char *zErr; int rc; rc = sqlite3_open("/usr/YY_AEACard.db", &db); if (rc) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return -1 ; } sqlcmd = sqlite3_mprintf("select CardID,CardType,BgnDate,EndDate,ValidTimes,CardRight,CardFlag, EnabledPorts,LastAccessPort,CardPsw from tb_CardInfo where CardID=%d and EnabledPorts = %d\n",card->CardID, nPort ); sqlite3_get_table( db, sqlcmd, &azResult, &nrow, &ncolumn, &zErr ); sqlite3_free( sqlcmd ); /////////////////////////////////////////////////////////// sqlite3_free_table(azResult); /////////////////////////////////////////////////////////// // it still works when i do this here. if (rc != SQLITE_OK) { if (zErr != NULL) { fprintf(stderr, "SQL error: %s\n", zErr); sqlite3_free(zErr); } } if(nrow <= 0) { printf("No data from db!\n"); return -1; } if(ncolumn < 9) { printf("Columns are not enough for card table!\n"); return -1; } bzero(card, sizeof(card)); card->CardID = atoi(azResult[ncolumn+0]); card->CardType = atoi(azResult[ncolumn+1]); strcpy(card->BgnDate, azResult[ncolumn+2]); strcpy(card->EndDate, azResult[ncolumn+3]); card->ValidTimes = atoi(azResult[ncolumn+4]); card->CardRight = atoi(azResult[ncolumn+5]); card->CardFlag = atoi(azResult[ncolumn+6]); card->EnabledPorts = atoi(azResult[ncolumn+7]); card->LastAccessPort = atoi(azResult[ncolumn+8]); strcpy(card->CardPsw, azResult[ncolumn+9]); sqlite3_free_table(azResult); //this is a test for the memory leak printf("%s\n", azResult[4]); //funny enough, it still works here. sqlite3_close(db); return 0; please help me, thank you.

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #2

      There is no guarantee that the memory pointed to is overwritten when it is released. In my experience, only a very few debugging memory managers bother to overwrite released memory blocks. So what you observe is not evidence of a memory leak. Your system may have some functions to query the memory manager for such things as the total allocated memory size. Cheers, Peter

      Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

      M 1 Reply Last reply
      0
      • P Peter_in_2780

        There is no guarantee that the memory pointed to is overwritten when it is released. In my experience, only a very few debugging memory managers bother to overwrite released memory blocks. So what you observe is not evidence of a memory leak. Your system may have some functions to query the memory manager for such things as the total allocated memory size. Cheers, Peter

        Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

        M Offline
        M Offline
        mimi0525
        wrote on last edited by
        #3

        Thank you Peter, we usually meet a "Segment fault" when we access to the memory released, and further more, the memory of my application keeps expanding with the size of the data loaded from database, never the end.

        P 1 Reply Last reply
        0
        • M mimi0525

          Thank you Peter, we usually meet a "Segment fault" when we access to the memory released, and further more, the memory of my application keeps expanding with the size of the data loaded from database, never the end.

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          My guess is that sqlite is keeping various things in memory as long as you have the database open. Is all the memory freed when you close the database(s)? Or, at least, does the memory usage stop increasing when you close and reopen the database? Peter

          Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

          M 2 Replies Last reply
          0
          • P Peter_in_2780

            My guess is that sqlite is keeping various things in memory as long as you have the database open. Is all the memory freed when you close the database(s)? Or, at least, does the memory usage stop increasing when you close and reopen the database? Peter

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            M Offline
            M Offline
            mimi0525
            wrote on last edited by
            #5

            Yes, Peter, SQL Server manages its memory this way, I don't exactly know how sqlite does with it. but what ever, it shouldn't expand to crash the operation system.

            1 Reply Last reply
            0
            • P Peter_in_2780

              My guess is that sqlite is keeping various things in memory as long as you have the database open. Is all the memory freed when you close the database(s)? Or, at least, does the memory usage stop increasing when you close and reopen the database? Peter

              Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

              M Offline
              M Offline
              mimi0525
              wrote on last edited by
              #6

              by the way, i closed the database after each opration.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups