Hi!
I'd like to create database based models, so I wanna use my own DatabaseModel class to manage the database connection, and every class that uses database is derived from it (it would be a mapping between the model and the table). I'm using a sqlite API.
Since I need only one database connection instance, I created a static variable to store the connection instance
DatabaseModel.h --------------- @interface DatabaseModel : NSObject { } // the connection instance static FMDatabase *database; +(BOOL) open; +(void) close; +(id)getDatabase; @end DatabaseModel.m --------------- // Is it necassary? static FMDatabase *database = nil; @implementation DatabaseModel +(BOOL) open { // make connection (doodled code) database = [DBAPI open]; } +(void) close { // ... } +(id)getDatabase { // Throws bad_memory_access [database retain]; return database; } @end MyClass.h --------- @interface MyClass : DatabaseModel { } -(void) foobar; @end MyClass.m --------- @implementation MyClass -(void) foobar { // This assign doesn't work database = [DatabaseModel getDatabase]; } @end
In this case [database retain] throws a bad_access exception. I don't understand exactly, when database is a static variable, why I get this message...