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:
Bulk.m:
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:
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:
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.
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:
This is the output:
So, my question is: WHY does it create two instances and how do I fix this?
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:
Code:
@interface Bulk : NSObject {
...
BOOL bulkDownloaderOpened;
}
@property BOOL bulkDownloaderOpened;
Bulk.m:
Code:
- (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];
[bulkWindow makeKeyAndOrderFront:self];
}
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:
Code:
-(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:
Code:
[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.
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:
Code:
NSLog(@"%@ - opened now set to: %d", self, [self bulkDownloaderOpened]);
Code:
[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[/b]>
2009-03-03 11:23:43.719 Lyricus[43280:10b] <Bulk: [b]0x1288020[/b]> - 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: [b]0x1288020[/b]> - 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: [b]0x123be50[/b]>, opened = 1
So, my question is: WHY does it create two instances and how do I fix this?