Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6682173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:40:31+00:00 2026-05-26T04:40:31+00:00

I spent hours trying to fix that but I’ve just given up; I have

  • 0

I spent hours trying to fix that but I’ve just given up; I have no idea what’s wrong.

In my app, I do nested SQL operations to set all my objects correctly. For some reason, sometimes the sqlite3 objects do not get release properly, causing the memory to go up the roof. I understand it is a problem with using correctly sql3_close and sql3_finalize. However, as you will see, I think I have used them correctly.

Here is the source of the problem:

- (NSArray *) getAllSessions {
    if (sqlite3_open(dbPath, &db) == SQLITE_OK) { 
        if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {

                //I found out that doing something like that
                //toAdd.in_loc = [self getIndoorLocationWithId:[NSNumber numberWithInt:(int)sqlite3_column_int(statement, 6)]];
                //messes the memory up all the time
                //but doing that works OK:
                NSNumber *_id = [[NSNumber alloc]  initWithInt:(int) sqlite3_column_int(statement, 5)];
                toAdd.out_loc = [self getOutdoorLocationWithId:_id];
                [_id release];

                //So I did the same with the second one, but this one messes memory up:
                NSNumber *id2 = [[NSNumber alloc] initWithInt:(int)sqlite3_column_int(statement, 6)];
                toAdd.in_loc = [self getIndoorLocationWithId:id2];
                [id2 release];
            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(db);
    } 
}

So here is the one that messes memory up:

- (IndoorLocation *) getIndoorLocationWithId:(NSNumber *) locId {
    if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) { 
        if (sqlite3_prepare_v2(db, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {  
                //if I comment the thing below it works
                NSNumber *_id = [[NSNumber alloc]  initWithInt:(int) sqlite3_column_int(statement, 5)];
                toReturn.outLoc = [self getOutdoorLocationWithId:_id];
                [_id release];

            }
            sqlite3_finalize(statement);
        }
        sqlite3_close(db);
    }
}

So in the one that messes memory up, I use exactly the same function as the first time (getOutdoorLocationwithId), in the same way but it doesn’t work, sqlite3 objects don’t get released properly.

I hope you understand my problem, this is driving me nuts!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:40:31+00:00Added an answer on May 26, 2026 at 4:40 am

    In a single user app like this, there’s no need to continually open and close. This is not like a server based app where you have connection pooling with multiple users where holding the connection can defeat the pool and hurt scalability. In a phone app open it and hold it open. close it when you’re done. At a minimum, do that within a recursive call.

    To make it worse, you’re opening and closing within recursive calls – just leave it open.

    Also:

    • You’re not checking the return codes of finalize
    • You’re not checking the return codes of close.
    • You’re preparing (compiling) the statments but you’re not saving them off – call reset on the saved statement and execute it again.

    Consider using FMDB – it’s a good wrapper.

    BTW, here’s a richer and more durable close but don’t use it on every call. Close it when you’re ending or your app is going into the background … This is mine which is similar to what fmdb does.

    - (void)close
    {
        if (_sqlite3)
        {
            ENInfo(@"closing");
            [self clearStatementCache];
    
            int rc = sqlite3_close(_sqlite3);
            ENDebug(@"close rc=%d", rc);
    
            if (rc == SQLITE_BUSY) 
            { 
                ENError(@"SQLITE_BUSY: not all statements cleanly finalized");
    
                sqlite3_stmt *stmt; 
                while ((stmt = sqlite3_next_stmt(_sqlite3, 0x00)) != 0) 
                {
                    ENDebug(@"finalizing stmt");
                    sqlite3_finalize(stmt); 
                }
    
                rc = sqlite3_close(_sqlite3);
            }
    
            if (rc != SQLITE_OK)
            {
                ENError(@"close not OK.  rc=%d", rc);
            }
    
            _sqlite3 = NULL;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue that I've spent hours trying to figure out, but since
I have literally spent hours trying to fix this but cant! Like the title
I'm sorry to ask this question but I have spent hours trying to understand
I have spent hours trying to get my project working and I just can't
I have a question that I have spent too many hours trying to resolve.
I have spent hours trying to fix this problem I have, basically I want
So yeah, like I said, I've spent a few hours trying to fix this
this may sound funny, but i spent hours trying to recreate a knob with
I have a problem with php header redirect. I already spent hours trying to
i spent last few hours trying to find what is wrong in my code(?)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.