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!
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;
}
}
I met the same issue.
When i use and open FMDB, it works OK. but in other callbacks it failed and threw exception of "Closing leaked statement." Finally I found that's because i kept the pointer from [FMDatabase databaseWithPath:dbPath] and the pointer is a autorelease object. I fixed the issue by this:
FMDatabase *db = [[FMDatabase databaseWithPath:dbPath] retain];
and when you want to close the database:
db [close];
[db release];
db = nil;
By this, you don't always open and close database for each operation, the should be a manager object accounting for it. When the manager starts, the database is open always until the manager stops.
Related
Updates
19th August 2015 - Bug seems to have now been fixed in the 3.13 update, even though the only thing they list in their changelog is "Fixed an issue which caused crashes when using setCampaignParametersFromUrl". Take that as you will.
8th June 2015 - Still encountering this problem. If I disable automatic sending of events ([GAI sharedInstance].dispatchInterval = -1;) then I still receive errors. I assume, therefore, that the problem lies with inserting the event into the Google Analytics SQLite database, that somehow my own database statement that is currently in progress are becoming void.
10th June 2015 - Still encountering crashes. Tried removing my controllers extending GAITrackedViewController and sending the createScreenView track manually with no change in crash frequency.
25th June 2015 - Still encountering crashes.
Intro
I have added the Google Analytics SDK 3.12 to my iPhone app and everything is working as expected - I run the app and can see all of the hits and events that I have setup coming through on the Web interface.
I am initialising the SDK in my AppDelegate right at the top of my didFinishLaunchingWithOptions, like so:
[[GAI sharedInstance] trackerWithTrackingId:GOOGLE_ANALYTICS_ID];
The problem
However, I have found that running Google Analytics creates errors when I try and use SQLite for myself. They can manifest as serious errors such as:
"Database disk image is malformed" and then insta-crashes
"Disc i/O error" whenever I run a query (though doesn't crash)
And they can also cause my own SQLite queries to just fail, for instance:
if (! sqlite3_prepare_v2(_db, [sql UTF8String], -1, &_statement, NULL) == SQLITE_OK) {`
// ..
// ..
if (sqlite3_step(_statement) == SQLITE_ROW) {
Will result, randomly, in the following error:
sqlite3_prepare_v2 EXC_BAD_ACCESS (code=1, address=0x6800000000)
If I comment out the SDK initialisation then everything goes back to being incredibly stable. Uncomment it again and it will crash the app within a minute.
Pre-emptive question answering
Am running this on a iPhone 6 running 8.3 (12F70).
Have tried uninstalling and reinstalling the app.
I have added all of the pre-requisites for Google Analytics to work; all of the .m files to the library, the libGoogleAnalyticsServices.a file, and also the Linked Frameworks and Libraries.
I also have Crashlytics, but have tried commenting it out from the code ([Fabric with:#[CrashlyticsKit]];) and removing its library from the Linked Frameworks and Libraries with exactly the same results.
Code
Setting up the class
// In didFinishLaunchingWithOptions
[Db setup];
[Db connect];
Accesses the class
Db * db = [[Db alloc] init];
if ([db prepare:#"SELECT * FROM `table` WHERE `id` = ?" withBindings:#[#"123"]]) {
while ([db stepThrough]) {
// ..
}
}
[db finalise];
The class
(Have indicated where the errors appear with comments)
#implementation Db
static sqlite3 * _db;
static NSString * _dbPath;
#pragma mark - Setup
+ (BOOL)setup {
NSString * sqlBundlePath = [[NSBundle mainBundle] pathForResource:#"db" ofType:#"sqlite"];
NSString * documentsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString * sqlDocumentPath = [[documentsFolder stringByAppendingPathComponent:#"db"] stringByAppendingPathExtension:#"sqlite"];
NSFileManager * fileManager = [NSFileManager defaultManager];
if (! [fileManager fileExistsAtPath:sqlDocumentPath]) {
NSError * error;
BOOL success = [fileManager copyItemAtPath:sqlBundlePath toPath:sqlDocumentPath error:&error];
if (! success) {
return NO;
}
}
_dbPath = sqlDocumentPath;
return YES;
}
+ (BOOL)connect {
sqlite3_config(SQLITE_CONFIG_SERIALIZED);
return sqlite3_open([_dbPath UTF8String], &_db);
}
#pragma mark - Querying
- (BOOL)prepare:(NSString *)sql withBindings:(NSArray *)bindings {
// ERROR CAN OCCUR ON THE FOLLOWING LINE
if (! sqlite3_prepare_v2(_db, [sql UTF8String], -1, &_statement, NULL) == SQLITE_OK) {
NSLog(#"Error whilst preparing query: %s", sqlite3_errmsg(_db));
sqlite3_finalize(_statement);
return NO;
}
for (int i = 0; i < [bindings count]; i++) {
sqlite3_bind_text(_statement,
i + 1,
[bindings[i] isKindOfClass:[NSNull class]] ? [#"" UTF8String] : [bindings[i] UTF8String],
-1,
SQLITE_TRANSIENT);
}
return YES;
}
- (BOOL)stepThrough {
// ERROR CAN OCCUR ON THE FOLLOWING LINE
if (sqlite3_step(_statement) == SQLITE_ROW) {
return YES;
}
sqlite3_finalize(_statement);
return NO;
}
- (void)finalise {
sqlite3_finalize(_statement);
}
#end
Upgrading to the new version of the SDK (3.13) fixed this issue (at least for me), even though their changelog does not specifically mention it.
I need to store user doubleTap count in db. Its working fine.
If i am doing very fast double tapping to insert, issues will appear (database is locked, no such table: and out of memory). How to handle that issue?
Thanks in Advance
I tried this, FYI:
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void)
{
#try {
if([self openAppDatabase] == YES) //If DB open success...
{
sqlite3_stmt *statement = nil;
NSString *queryString = [NSString stringWithFormat:#"INSERT INTO %# (%#,%#) values (?,?)",Tables_Count,lkt_TO,ttt_TIME];
BOOL prepareStatementResult = sqlite3_prepare_v2(databaseObj, [queryString UTF8String], -1, &statement, NULL);
if(prepareStatementResult == SQLITE_OK) {
sqlite3_bind_text(statement, 1, [contactNo UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [sentDate UTF8String], -1, SQLITE_TRANSIENT);
if (sqlite3_step(statement) != SQLITE_DONE) {
DLog(#"Can't insert ---- Error");
}
}
else {
// In the database cannot be opened then show the error message on the debugger.
DLog(#"%s", sqlite3_errmsg(databaseObj));
}
// Release the compiled statement from memory.
sqlite3_finalize(statement);
}
[self closeAppDatabase]; //If DB not opened will close it here...
}
#catch (NSException *exception) {
DLog(#"#Exception --- %#",exception.reason);
}
}
The dispatch queue you are using is a concurrent queue, not a series queue. The result is that too many requests are being made to the dB at a time. You should be ok if you change your code to use dispatch_barrier_async but I've never used it on system queues.
Additionally, you're code could use some cleanup. The try is unnecessary since this code does not throw exceptions. Contrary to Java, Objective Chicago very rarely throws exceptions.
Lastly, you may consider using a Objective C SQLite wrapper, like FMDatabase. It cleans things up quite a bit.
I'm having an issue opening my sqlite database for an iPhone app I'm writing. I thought I followed the tutorials verbatim but for some reason I am getting an "Out of memory" error.
-(NSString *) filepath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex:0] stringByAppendingPathComponent:#"db.sqlite"];
}
-(sqlite3*)openDB{
if(db == NULL){
sqlite3 *newDBConnection;
if(sqlite3_open([[self filepath] UTF8String], &newDBConnection) != SQLITE_OK){
sqlite3_close(db);
NSLog(#"%s SQL error '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(db), sqlite3_errcode(db));
db = NULL;
}
else{
NSLog(#"db opened");
}
}
return db;
}
DB is an ivar and I am calling db = [self openDB]; in the initialization method.
The sqlite3_open is failing because you are using NSDocumentationDirectory instead of NSDocumentDirectory.
The reason you're receiving the "Out of memory" error is that sqlite3_open is updating newDBConnection, but the sqlite3_errmsg is trying to use db (which is still NULL). And whenever you call sqlite3_errmsg with a NULL for the sqlite3 pointer, SQLite somewhat confusingly returns an "Out of memory" message.
Also note that even if you fix the two above issues, note that you are calling sqlite3_errmsg after performing sqlite3_close. Make sure to get your error message before you call sqlite3_close.
E.g. I would suggest:
- (BOOL)openDB {
if (db == NULL) {
int rc;
if ((rc = sqlite3_open([[self filepath] UTF8String], &db)) != SQLITE_OK) {
NSLog(#"%s SQL error '%s' (%d)", __FUNCTION__, sqlite3_errmsg(db), rc);
sqlite3_close(db);
db = NULL;
return false; // open failed
} else {
NSLog(#"db opened");
return true; // open successful
}
}
return true; // already open
}
Note, a minor point, given that sqlite3_open returns the error code, I would just save that directly, rather than calling sqlite3_errcode to get the code that was just returned.
Iam inserting the contacts from the address book in to the sqlite3 database. for only some contacts my app is getting crashed at
sqlite3_finalize(statement)
this statement. and this is happening in ios7. The crash log is as follows
"Critical failure: the LastResort font is unavailable. in ios"
NSString *insertSQL = [NSString stringWithFormat:#"insert into vcards (some thing data)",(something data)];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"Record inserted");
}
else {
NSLog(#"Record inserted failed");
}
sqlite3_finalize(statement);
sqlite3_close(database);
please any one suggest me how to fix this crash.
This has nothing to do with your contacts but to do with the fonts when transitioning from ios 6 to ios 7. Delete all the fonts in your project and then carefully add one by one to your .plist testing each time before adding the next until you find the one that is causing the problem.
Have a look at this post from someone who also had the same problem as you and the person's answer.
Person have the same problem as you
I'm trying to follow this tutorial: http://sqlcipher.net/ios-tutorial/
I create a database called "sqlcipher.db" then I recreate this
When I execute this code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent: #"encrypted.db"];
sqlite3 *db;
if (sqlite3_open([databasePath UTF8String], &db) == SQLITE_OK) {
const char* key = [#"secret" UTF8String];
int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
sqlite3_key(db, key, strlen(key));
if (sqlite3_exec(db, (const char*) "SELECT count(*) FROM t1;", NULL, NULL, NULL) == SQLITE_OK) {
// password is correct, or, database has been initialized
NSLog(#"Hello 1");
} else {
// incorrect password!
NSLog(#"Hello 2");
}
sqlite3_close(db);
} else {
NSLog(#"Hello 3");
}
}
It allways outs "Hello 2".
When I try to reproduce the steps to crate an encrypted db described here http://zetetic.net/blog/2009/12/29/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher.html#disqus_thread I can't get it encrypted, I believe that it is beacause I am using sqlite3 mac command.
So I saw in the comments that S Lombardo says that I have to compile a command line sqlcipher executable but the link doesn't works.
How should I encrypt my database to use it with SQLcipher?
Did anyone have success using sqlicipher in iOS?
After one hour Googling i've found how to compile sqlcipher command line for OSX:
I hope this could help somebody.
https://groups.google.com/forum/#!msg/sqlcipher/bd1R13RpZHQ/SEPK8YrRt1gJ