Re: SIGBUS/SIGSEGV error problems
Re: SIGBUS/SIGSEGV error problems
- Subject: Re: SIGBUS/SIGSEGV error problems
- From: Pete Yandell <email@hidden>
- Date: Tue, 12 Aug 2003 11:44:41 +1000
On Tuesday, August 12, 2003, at 11:18 AM, Mike Brinkman wrote:
- (void)updateSum
{
int i; // counter
float sum; // sum of NSNumbers
XObject *myObject;
sum = 0;
for (i = 0; i < [myArray count]; i++)
{
myObject = [[XObject alloc] init];
So you set myObject to be a pointer to a new object...
myObject = [myArray objectAtIndex:i];
...then you replace that pointer with an object retrieved from your
array, thereby leaking the memory for the object you've just
allocated...
// increment sum with floatValue of current object's amount
sum += [[myObject amount] floatValue];
// release myObject
[myObject release];
...then you release the object stored in the array which you haven't
retained, hence the crash.
}
[sumField setFloatValue:sum];
}
If you just took out a few lines and did:
sum = 0;
for (i = 0; i < [myArray count]; i++)
sum += [[myArray objectAtIndex:i] floatValue];
it would all work fine.
Pete Yandell
http://pete.yandell.com/
_______________________________________________
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.