0

I have written my own class (subclass of a NSObject) and have also written a custom initializer for it. The problem I am running into is, when I start my application, and call the refreshData instance method, everything works as it should. When I then call the refreshData method for a second time, the object is deallocated and I get the error. When the instance is initialized, the memory is allocated but somehow it is being deallocated. What am I missing?

UIViewController:

- (void)viewDidLoad {

    [super viewDidLoad];

    // Initialze an instance of our data controller class
    dataController = [FCDataController initWithObject:self animated:NO];
    [dataController refreshData];
}

NSObject:

+ (FCDataController *)initWithObject:(id)object animated:(BOOL)animated {

    FCDataController *dataController = [[FCDataController alloc] initWithObject:object animated:animated];

    return dataController;
}

- (id)initWithObject:(id)object animated:(BOOL)animated {

    self = [super init];

    if (self) {

        self.delegate = object;
        self.animated = animated;
    }
    return self;
}
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73
  • are you using ARC? (assuming yes, but doesn't hurt to ask) Also, if this is a subclass of `NSObject` and not `UIViewController` how is `viewDidLoad` going to get called? – DBD Feb 19 '13 at 19:06
  • I see how `[self refreshData]` is getting called in the init. How is it getting called the second time? Chances are.. whatever creates this class isn't holding onto the reference and that's how it gets deallocated. – DBD Feb 19 '13 at 19:11
  • The second time, at the action of a button, [dataController refreshData]; is being called. dataController, being the instance variable set in viewDidLoad. – Jon Erickson Feb 19 '13 at 19:12
  • 3
    How's dataController declared? – Attila H Feb 19 '13 at 19:19
  • as a property: @property (strong, nonatomic) FCDataController *dataController; – Jon Erickson Feb 19 '13 at 19:20
  • Maybe you can copy/paste the exact error and the stack trace? – Firoze Lafeer Feb 19 '13 at 19:39
  • I am just going to initialize it through an instance method, instead of a class method. I know it works that way. – Jon Erickson Feb 19 '13 at 19:43
  • Ok. The multiple files makes a lot more sense. In the `UIViewController`, what does your `dataController` declaration look like? If it's a "weak" reference then it your `NSObject` class will get deallocated. It needs to be a "strong" reference. – DBD Feb 19 '13 at 19:44
  • Look at 3 comments above :) – Jon Erickson Feb 19 '13 at 19:51

1 Answers1

0

viewDidLoad will only get called on a sub-class of UIViewController. Is this class really a sub-class of a UIViewController?

Making this assumption this is a view controller, an instance of the class will already have been created and be running when viewDidLoad is called. Your line inside viewDidLoad will create a second instance of the same class assigning the first instance to be the delegate of the second instance. This is probably not what you want.

You probably would like to just refresh the data when the view is loaded.

- (void)viewDidLoad {
    [super viewDidLoad];

    // Initialze an instance of our data controller class
    [self refreshData];
}

To extend it a bit further, you probably don't need to refresh the data on init because you are just going to refresh it again when the view loads.

DBD
  • 23,075
  • 12
  • 60
  • 84
  • No, its not a subclass of a UIViewController. Its a subclass of NSObject. And adding the [self refreshData] to viewDidLoad and taking it out of the initializer does not work. Still get the same error. – Jon Erickson Feb 19 '13 at 19:28
  • Also, it would be [dataController refreshData]. My UIViewController doesnt have a refreshData instance method. – Jon Erickson Feb 19 '13 at 19:30
  • how is `viewDidLoad` getting called? Are you calling it manually somewhere in your code? – DBD Feb 19 '13 at 19:30
  • viewDidLoad is called in the UIViewController class. Within that class, I create an instance of my data controller class which then I use to call the refreshData instance method. – Jon Erickson Feb 19 '13 at 19:31
  • If it makes more sense, the code up above is from 2 different files, one is a UIViewController and the other, the NSObject. – Jon Erickson Feb 19 '13 at 19:32