Re: resetting ivars safely
Re: resetting ivars safely
- Subject: Re: resetting ivars safely
- From: Daniel Child <email@hidden>
- Date: Wed, 12 Sep 2007 21:44:17 -0400
Actually, I originally HAD [NSMutableArray array], because I thought
it would be good to have them autoreleased. But when I ran into EXC-
BAD-ACCESS, I thought perhaps I was losing them in an autorelease
that came too early. Since each time I reset, I release the array,
each alloc is matched with a release, at least as far as I can see.
Here's the sequence:
create word parser (one time only)
init word parser, and in so doing, init the wordCandidate field as
follows:
[self setWordCandidates: [[NSMutableArray alloc] init]]; // ONE ALLOC
after done looking at the word candidates, reset the parser so I can
process new input, i.e.
[self setWordCandidates: nil];
which of course calls
- (void) setWordCandidates: (NSMutableArray *) wc
{
if (wc != wordCandidates) {
NSLog(@"wordCandidates == %@\n", wordCandidates);
printf("wordCandidates retain count prior to release is %i.\n",
[wordCandidates retainCount]);
[wordCandidates release]; // ONE RELEASE
wordCandidates = [wc mutableCopy];
}
}
The release and allocs are balanced, as far as I can tell.
the only thing I can think is that the wordCandidates holds
NSStrings. You never really "set" the array, but rather add
candidates as they are calculated. Here's that method.
/
************************************************************************
*********
** addCandidate
**
** adds a candidate to the array of wlrdCandidates
************************************************************************
*********/
- (void) addCandidate: (NSString *) c
{
[[self wordCandidates] addObject: c]; // wordCandidates is an
NSMutableArray
// ALSO TRIED [wordCandidates addObject: c];
// DO I NEED TO COPY OR RETAIN HERE?
}
I wonder if this is where the problem is, but since there were four
objects registered at the time wc release is called, I discounted
it.....
On Sep 12, 2007, at 9:28 PM, Shawn Erickson wrote:
On 9/12/07, Daniel Child <email@hidden> wrote:
The first time, retain count is 1, and the objects are all there (4
objects in the array).
Later on, the retain count is 0, and there are no objects.
But doesn't that mean there is a problem with the setters paradigm of
"release then copy"?
In the code you posted I don't see anything that would explain the
issue you report at least not the way you have reported.
Note you should switch to use [NSMutableArray array] instead of
[[NSMutableArray alloc] init] in your classes init method (the
setSyllables: and setWordCandidates:) lines. Review the memory
documentation to understand why (hint you are leaking those initial
instances)
-Shawn
_______________________________________________
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