Search code examples
iosobjective-cios6thread-safetydispatch-async

EXC_BAD_ACCESS on dispatch_async while inserting data into SQLite with FMDB


So, I've read through every posts I found on internet but i still can't seem to make this work.

I'm trying to insert a huge amount of data into sqlite database. It's 20000 rows of data, so I have to do it in the background thread.

I have a NSObject .h and .m files to handle the database operations. And I call them from inside my main view.

Here's my code :

SQLiteDBHandler.m :

 database = [FMDatabase databaseWithPath:[self getDBPath]];
    [database open];
    dispatch_queue_t q = dispatch_queue_create("FMDBQueue", NULL);
    dispatch_async(q, ^{
        for(Customer *c in arrayOfObjects) {
            [database executeUpdate:@"INSERT INTO SNP(rdis, Position, FirstName, LastName) VALUES (?,?,?,?)", c.ID, c.Position, c.FirstName, c.LastName];
        }
        [database close];
    });

and for calling the method in the main view, I call it this way :

SQLiteDBHandler *dbHandler = [[SQLiteDBHandler alloc]init];

[dbHandler insertDataIntoTable:mutableArray];

I've tried changing the FMDatabase with FMDatabaseQueue with no luck. So any help would be highly appreciated because I'm pretty desperate in this.

Thanks. Cheers!


Solution

  • EXC_BAD_ACCESS means you have a zombie, enable zombie objects and use the instruments tool and it will show you where the zombie object is. This link here is a good tutorial on how to detect them

    http://www.raywenderlich.com/2696/how-to-debug-memory-leaks-with-xcode-and-instruments-tutorial

    Hope that helps

    EDIT:

    In xCode, toolbar at top of the screen Product->Edit Scheme->Diagnostics-> Click Enable Zombie Objects

    Then select product on the toolbar at top of the screen, and go down to profile, and instruments should appear, and you should see an option for zombie, select that.

    Edit2:

    Forgot to say, the zombie template will only appear if you profiling on the simulator, it won't appear if you try profiling on an actual device

    Edit3: Pics

    enter image description here

    Then you should see this

    enter image description here

    Then when you run the app, navigate to the screen where the error occurs, and the app should stop, and this should appear

    enter image description here

    Click the arrow beside the error message, and it should you show in the stack trace where the error is occuring