marcoarment / FCModel

An alternative to Core Data for people who like having direct SQL access.
MIT License
1.65k stars 173 forks source link

Crash on closeDatabase due to g_instancesReadLock hasn't been initialised #89

Closed herlihalim closed 10 years ago

herlihalim commented 10 years ago

If you open a database and close it before any operation is done to it. It'll crash due to g_instancesReadLock hasn't been initialised yet, as there hasn't been any chance to call uniqueMapInit yet.

NSString *dbPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"testDB.sqlite3"];
[FCModel openDatabaseAtPath:dbPath withSchemaBuilder:^(FMDatabase *db, int *schemaVersion) {
}];
[FCModel closeDatabase];
ahti commented 10 years ago

Just ran into this issue, too.

Since g_instancesReadLock and g_instances seem to be designed to stick around forever after they have been created and since creating a semaphore and a dictionary is very inexpensive, I think it would be reasonable to just initialise them in +initialize.

This would also mean we could remove the several calls to +uniqueMapInit scattered across various FCModel methods.

A temporary workaround for anyone facing this problem right now would be to just declare +[FCModel uniqueMapInit] somewhere and call it when you open your database (no worries, it won't do any damage if it's called multiple times).