Re: simple array release error question
Re: simple array release error question
- Subject: Re: simple array release error question
- From: David Remahl <email@hidden>
- Date: Sun, 1 Jun 2003 01:36:50 +0200
You do not understand Cocoa memory management on a fundamental level.
In the first example, you add ten objects. Their retain counts are
increased by the array. Then you remove them from the array - their
retain count is decremented and probably reaches zero (unless you've
retained them before). Then you release the array.
In the second example, you add ten objects, whose retain count is
increased by the array. Then you iterate through them and decrease
their retain count. Their retain count reaches zero, and they are
released. But they are still present in the array, so when you release
the array, they get a second release, which - since they are already
disposed of - causes a crash.
What I think you want is:
for( ... ) {
[array addObject:entry];
}
for( ... ) {
[array removeObjectAtIndex:0];
}
[array release];
/ Regards, David
On svndag, jun 1, 2003, at 01:18 Europe/Stockholm, Ben Dougall wrote:
why is the first version here fine, and the second version not?
for(i = 0; i < 10; i++) {
...
[array addObject: entry];
}
...
[array removeAllObjects];
[array release]; // no errors
___________________
for(i = 0; i < 10; i++) {
...
[array addObject: entry];
}
...
for(i = 9; i >= 0; i--)
[[array objectAtIndex:i] release];
[array release]; // this line gives this error: signal 11 (SIGSEGV)
array doesn't seem to exist when [array release] is called in the
second one. why is it different to the first version?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.