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 553559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:37:04+00:00 2026-05-13T11:37:04+00:00

I have the following function in my iPhone project which works great…unless the query

  • 0

I have the following function in my iPhone project which works great…unless the query returns nothing and then the app crashes. It is being a pain to debug with none of the breakpoints being activated at all!

I know this works as I pass in static stuff that is in the DB and it returns a value.

-(NSString *)getSomeText:(NSString *)toPass {
    sqlite3 *database;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"sf.sqlite"];

    int strLength = 0;
    strLength = [toPass length];

    if (strLength <3)
        return @"Unknown";


    NSString *MIDstr;
    NSMutableString * toPass Copy = [NSMutableString stringWithString:toPass];
    MIDstr = [toPassCopy substringWithRange:NSMakeRange(0, 3)];


    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        NSString *BaseSQL = [NSString stringWithFormat:@"select * from MIDS where MID = '%@'",MIDstr];
        NSLog(BaseSQL);

        const char *sqlStatement = [BaseSQL UTF8String];
        //NSLog(BaseSQL);
        sqlite3_stmt *compiledStatement;
        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array

            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {


                    NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
                    NSString *returnString = [NSString stringWithFormat:@"%@",aName];
                    return returnString;                

            }
        }
        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);

}
  • 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-13T11:37:05+00:00Added an answer on May 13, 2026 at 11:37 am

    A. if sqlite3_step does not return any rows, you crash because you have declared that you are returning a NSString, but when there are no rows you return nothing.
    The caller will try to read a NSString from the stack and thus end up dereferencing garbage.

    To quickly fix the problem, write:

        sqlite3_close(database);
        return nil;
    }
    

    and make sure the caller handles nil results.

    B/ If you do have data, your code never gets to call sqlite3_finalize and sqlite3_close because you return early:

    while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
        [..]
        return returnString;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an iPhone app which uses iAd...The app crashes sometime with the following
I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
I have developed a Cocos2d-X app with XCode which works perfectly when I launch
I have the following code in my iPhone app CGPoint p = [StaticClass doSomethingWithA:a
We have a Rails app which communicates with a PhoneGap-based iPhone app. We are
I started with a Universal app project but have changed it to iPhone only
I have the following code in PhoneGap iPhone, in file Geofence.js Geofence.prototype.subscribeToLayer = function(layerName,circularRadius,successCallback){
I have the following function that is pulling data from a database. The ajax
I have the following function: //Function to get random number public static int RandomNumber(int
I have the following function: - (NSString *)urlEncodedValue { NSString *result = (NSString *)CFURLCreateStringByAddingPercentEscapes(

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.