Re: resetting ivars safely
Re: resetting ivars safely
- Subject: Re: resetting ivars safely
- From: "Shawn Erickson" <email@hidden>
- Date: Wed, 12 Sep 2007 19:24:00 -0700
On 9/12/07, Daniel Child <email@hidden> wrote:
> 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:
Lets flatten this out...
{
NSMutableArray* mutableArray = [[NSMutableArray alloc] init]; //
implicit retain because of alloc
// you now have a mutable array instance that this code block "owns"
[self setWordCandidates:mutableArray];
// The implicit retain of mutableArray is not balanced with a
release or autorelease
// this code is NOT following proper Cocoa memory management as a result
// This is a bug that should be fixed since it results in leaked
array instances
}
- (void) setWordCandidates: (NSMutableArray *) wc
{
if (wc != wordCandidates) {
// wc doesn't reference the same objects as wordCandidates so...
[wordCandidates release];
// The above releases "ownership" of the object that
wordCandidates currently
// references. If wordCandidates is nil this line does
nothing. Note wordCandidates
// will be nil when your singleton object is first alloc/inited.
// The above line balances the implicit retain resulting in
the line below
// (in a future call to this method).
wordCandidates = [wc mutableCopy]; // implicit retain because of "copy"
// The above copies wc resulting in a new object that you now
"own" and makes
// wordCandidates reference that new object. Note if wc is nil
then wordCandidates
// will be set to nil as well.
}
}
- (void) addCandidate:(NSString *) c
{
[[self wordCandidates] addObject: c];
// The above will add the NSString instance referenced by c to the
// mutable array referenced by wordCandidates. If wordCandidates is nil
// the line does nothing. Note when an object is added to an array the
// array will retain the object so that it will continue to
exist while in the array.
}
Again nothing in the code you have posted above will result in the
issue I think you are reporting.
If you cannot figure this out using the debugger, logging and review
of the memory management documents then you will need to post a
complete example that demonstrates the issue you are hitting (ideally
one that will compile and run showing the error).
Also at the moment it is cloudy on exactly what issue(s) you are
hitting given all of the talk about things you have tried and the
various symptoms you reported.
We want to help but we don't have enough of picture to understand what
you are seeing...
-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