• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: SIGBUS/SIGSEGV error problems
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

References: 
 >SIGBUS/SIGSEGV error problems (From: Mike Brinkman <email@hidden>)

  • Prev by Date: Re: SIGBUS/SIGSEGV error problems
  • Next by Date: Re: SIGBUS/SIGSEGV error problems
  • Previous by thread: Re: SIGBUS/SIGSEGV error problems
  • Next by thread: Re: SIGBUS/SIGSEGV error problems
  • Index(es):
    • Date
    • Thread