resetting ivars safely
resetting ivars safely
- Subject: resetting ivars safely
- From: Daniel Child <email@hidden>
- Date: Wed, 12 Sep 2007 16:59:14 -0400
HI All,
I am trying to find a way to safely reset or reinitialize the
instance variables of a singleton object. A "word parser" behaves
correctly the first couple of times it is used, but then self-
destructs. Everything goes fine the first time through, but on the
second or third attempt to reset the wordCandidates ivar, an EXC-BAD-
ACCESS signal is generated just prior to releasing the ivar in its
setter (using the release, then copy paradigm). Since the retain
count prior to releasing is 1, I am mystified.
Here is the relevant code. Thanks much....
Daniel
+ (WordParser *) sharedWordParser
{
if (wordParser == nil) {
wordParser = [[WordParser alloc] init];
}
return wordParser;
}
- (id) init
{
if (self = [super init]) {
[self setSyllables: [[NSMutableArray alloc] init]];
[self setWordCandidates: [[NSMutableArray alloc] init]];
}
return self;
}
// triggered when a GUI reset button is pressed
- (void) reset
{
[self setSyllables: nil]; // identically structured ivar has no
problem for some reason
[self setWordCandidates: nil]; // PROBLEM OCCURS HERE
[self setCurrentCandidate: nil];
[self setHead: nil];
[self setTail: nil];
[self init];
}
// setter for wordCandidates ivar
- (void) setWordCandidates: (NSMutableArray *) wc
{
if (wc != wordCandidates) {
printf("wordCandidates retain count prior to release is %i.\n",
[wordCandidates retainCount]); // equal to 1
[wordCandidates release]; // EXC-BAD-ACCESS COMES HERE
wordCandidates = [wc mutableCopy];
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden