Fixing FMDB crashes on iPhone
I was trying to use the FMDB wrapper for sqlite in a simple iphone app, but I found that I would get a crash whenever I created a DB in one function and then use it again in a later one. I eventually realized that the memory region that was supposed to be holding my DB was being freed up without me knowing it. To fix the problem, I edited the FMDatabase.m file as follows:
+ (id)databaseWithPath:(NSString*)aPath {
//return [[[self alloc] initWithPath:aPath] autorelease]; REMOVE CALL TO AUTORELEASE
return [[self alloc] initWithPath:aPath];
}
For some reason the library sets the database object to be autorelease’d, a concept that I don’t fully understand in objective C. In any case, removing the autorelease flag makes the program work correctly. This may be adding a slight memory leak, but since I only create one database object ever, I don’t think it really matters for my purposes…