1

this is a follow-up question to my last one here: iOS: Initialise object at start of application for all controllers to use .

I have set my application up as follows (ignore the DB Prefix):

DBFactoryClass     // Built a DataManaging Object for later use in the app
DBDataModel        // Is created by the factory, holds all data & access methods
DBViewControllerA  // Will show some of the data that DBDataModel holds
moreViewControllers that will need access to the same DBDataModel Object

i will go step by step through the application, and will then in the end post the error message i get when building.

AppDelegate.h

#import "DBFactoryClass.h"

AppDelegate.m

- (BOOL)...didFinishLaunching...
{
    DBFactoryClass *FACTORY = [[DBFactoryClass alloc ]init ];
    return YES;
}

DBFactoryClass.h

#import <Foundation/Foundation.h>
#import "DBDataModel.h"

@interface DBFactoryClass : NSObject
@property (strong) DBDataModel *DATAMODEL;
@end

DBFactoryClass.m

#import "DBFactoryClass.h"

@implementation DBFactoryClass
@synthesize DATAMODEL;

-(id)init{
    self = [super init];
    [self setDATAMODEL:[[DBDataModel alloc]init ]];
    return self;
}

@end

ViewControllerA.h

#import <UIKit/UIKit.h>
#import "DBDataModel.h"

@class DBDataModel;
@interface todayViewController : UIViewController
@property (strong)DBDataModel *DATAMODEL;
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@end

ViewControllerA.m

#import "todayViewController.h"

@implementation todayViewController 
@synthesize testLabel;
@synthesize DATAMODEL;

- (void)viewDidLoad
{
    todaySpentLabel.text = [[DATAMODEL test]stringValue];
}
@end

DBDataModel.h

#import <Foundation/Foundation.h>

@interface DBDataModel : NSObject
@property (nonatomic, retain) NSNumber* test;
@end

DBDataModel.m

#import "DBDataModel.h"

@implementation DBDataModel
@synthesize test;
-(id)init{
    test = [[NSNumber alloc]initWithInt:4];
    return self;
}
@end

when i build it, i get the following error: EXC_BAD_ACCESS in this line:

@synthesize DATAMODEL;

of DBFactoryClass.m

Community
  • 1
  • 1
Sebastian Flückiger
  • 5,525
  • 8
  • 33
  • 69
  • 1
    As you know, what `@synthesize` does is to automatically generate implementations of the accessors for a property. EXC_BAD_ACCESS there means that you're accessing garbage when one of the accessors is executed. That's probably happening when you do this: `[self setDATAMODEL:[[DBDataModel alloc]init ]];`. Make sure that `[[DBDataModel alloc]init ]]` actually returns a legitimate object. – yuji Feb 26 '12 at 11:35
  • hi. this helped, thanks! the problem was a missing return self in the DBDataModel init function... (probably to tired^^) i updated all files in the first post - and since it builds fine now i got a new problem. i added a testNumber and Label to the project (number to the datamodel, label to the viewcontroller), to see if it actually works, and it doesnt. somehow i cant retrieve the data from the model. ideas? – Sebastian Flückiger Feb 26 '12 at 11:49
  • Glad that helped; reposting it as an answer. As for your other point, should post it as a separate question. – yuji Feb 26 '12 at 12:00
  • thanks - thats what i did, the new question is here: http://stackoverflow.com/questions/9452906/no-access-to-global-instance-build-by-factory-on-ios – Sebastian Flückiger Feb 26 '12 at 12:07

2 Answers2

2

What @synthesize does is to automatically generate implementations of the accessors for a property. EXC_BAD_ACCESS there means that you're accessing garbage when one of the accessors is executed.

That's probably happening here:

[self setDATAMODEL:[[DBDataModel alloc]init ]]; 

Make sure that DBDataModel's implementation of init actually returns a legitimate object.

yuji
  • 16,695
  • 4
  • 63
  • 64
0

As far as I can tell, your DBFactoryClass class is never stored anywhere, and therefore released right after the allocation if you use ARC (Since you use the strong keyword I assumed you do).

- (BOOL)...didFinishLaunching... {
  DBFactoryClass *FACTORY = [[DBFactoryClass alloc ]init ];
  // If you use ARC this might be released right afterwards
  return YES;
}

If you want the factory to be a singleton, use something like this

+ (id)sharedInstance {
  static dispatch_once_t once;
  static MyFoo *instance;
  dispatch_once(&once, ^{
    instance = [[self alloc] init];
  });
  return instance;
}
  • i dont really want to store the factory, just the DBDataModel object that is created by the factory. i want to use the factory to create more objects of different kinds that will be used throughout the application. – Sebastian Flückiger Feb 26 '12 at 11:51
  • &yes i use ARC. i was building the structure according to zoul's answer & example project from the post: http://stackoverflow.com/questions/9427101/ios-initialise-object-at-start-of-application-for-all-controllers-to-use/9427498#comment11941293_9427498 – Sebastian Flückiger Feb 26 '12 at 11:52