PDA

View Full Version : Cocoa: Instance variable changing on its own?




exscape
Mar 3, 2009, 04:19 AM
UPDATED, see the end: two instances of the class are created - but I don't know why or how to solve that.

This is one of those lovely times where you debug and debug and find NOTHING that could possibly be wrong, so I'm asking for some help.
Long story short: I have a main controller, for the whole application, LyricreaderController, that among many other things can open an instance of the Bulk class, and then loads a NIB and assigns ownership to that class.
In the Bulk class, I have the following code (obviously cut down):
Bulk.h:

@interface Bulk : NSObject {
...
BOOL bulkDownloaderOpened;
}

@property BOOL bulkDownloaderOpened;


Bulk.m:

- (BOOL)windowShouldClose:(id)sender {
// No need to confirm if nothing is running
if (![thread isExecuting]) {
[self setBulkDownloaderOpened:NO];
return YES;
}
else {
// No, don't abort
return NO;
}
}

-(void) awakeFromNib {
[self setBulkDownloaderOpened:NO];
// ...
[self openBulkDownloader];
}

-(void)openBulkDownloader {
...
[self setBulkDownloaderOpened:YES];
;
}


That's it; NO other lines of code change the bulkDownloaderOpened variable ANYWHERE in the entire program. Here's where it's read, and where the bug occurs, in

LyricreaderController.m:

-(IBAction)openBulkDownloader:(id)sender {
NSLog(@"in openBulkDownloader: %@, opened = %d", bulkDownloader, [bulkDownloader bulkDownloaderOpened]);
if (bulkDownloader == nil) {
bulkDownloader = [[Bulk alloc] init];
}

if ([bulkDownloader bulkDownloaderOpened] == NO) {
NSLog(@"%@", bulkDownloader);
[NSBundle loadNibNamed:@"Bulk" owner:bulkDownloader];
// [bulkDownloader openBulkDownloader]; // This is invoked anyway
}
}


When running the program, the bulk window opens just fine the first time, and closes just fine, but after closing, [bulkDownloader bulkDownloaderOpened] keeps returning YES in the LyricreaderController.
Here's the console output:

[Session started at 2009-03-03 11:14:39 +0100.]
---- I click to open the bulk downloader
2009-03-03 11:14:47.357 Lyricus[43238:10b] in openBulkDownloader: (null), opened = 0
2009-03-03 11:14:47.358 Lyricus[43238:10b] <Bulk: 0x1268590>
---- It's now open. I now close it, which generates no NSLog message (but I have set breakpoints on ALL setBulk...: lines and made sure that the :NO line is ALWAYS the last one invoked)
---- I now try to open it again, after closing it, twice, just to show that it's the same instance:
2009-03-03 11:15:49.241 Lyricus[43238:10b] in openBulkDownloader: <Bulk: 0x1268590>, opened = 1
2009-03-03 11:15:50.054 Lyricus[43238:10b] in openBulkDownloader: <Bulk: 0x1268590>, opened = 1


WHY is it returning YES even though the VERY LAST THING done in the Bulk class is to set the variable to NO? Please help me out here before I go bald. :D

EDIT: Uhhmm. After further debugging, I can see that two instances are created, and that the wrong instance is used, so to speak.
I added this line after EVERY setBulk...: call:

NSLog(@"%@ - opened now set to: %d", self, [self bulkDownloaderOpened]);

This is the output:

[Session started at 2009-03-03 11:23:41 +0100.]
2009-03-03 11:23:43.706 Lyricus[43280:10b] in openBulkDownloader: (null), opened = 0
2009-03-03 11:23:43.706 Lyricus[43280:10b] <Bulk: [b]0x123be50>
2009-03-03 11:23:43.719 Lyricus[43280:10b] <Bulk: 0x1288020> - opened now set to: 0
2009-03-03 11:23:43.837 Lyricus[43280:10b] <Bulk: 0x1288020> - opened now set to: 1
2009-03-03 11:23:43.864 Lyricus[43280:10b] <Bulk: 0x123be50> - opened now set to: 0
2009-03-03 11:23:43.993 Lyricus[43280:10b] <Bulk: 0x123be50> - opened now set to: 1
2009-03-03 11:23:44.018 Lyricus[43280:10b] <Bulk: 0x123be50> - opened now set to: 1
--- All the lines above are from opening it ONCE. I now click the close button:
2009-03-03 11:24:25.195 Lyricus[43280:10b] <Bulk: 0x1288020> - opened now set to: 0
--- And, again, I try to open it:
2009-03-03 11:24:41.926 Lyricus[43280:10b] in openBulkDownloader: <Bulk: 0x123be50>, opened = 1


So, my question is: WHY does it create two instances and how do I fix this?



kainjow
Mar 3, 2009, 08:56 AM
I'm guessing you're instantiating the class in your nib. Your nib probably should just have the File Owner set to your Bulk class, and not have a separate object in there.

exscape
Mar 3, 2009, 10:03 AM
I can't believe I've gone through it so many times and missed that. Yup, you're entirely correct. However, this caused a new problem. I connected all the outlets (I think!) to File's owner, which is set to class "Bulk". The "Go" button appears to work, but that's all I can verify since the tableView (the central part of the window) does NOT work. I did set the delegate to File's owner, and did the same in awakeFromNib, but to no avail. The tableView methods (- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView etc.) aren't getting called. Hmm.
Update: Ah, I found a little thing called dataSource. ;)
Still not working completely, but I'm getting there!

Edit: Yeah, it works!! Simple but genius. Thanks for the tip :)