Re: SIGBUS/SIGSEGV error problems
Re: SIGBUS/SIGSEGV error problems
- Subject: Re: SIGBUS/SIGSEGV error problems
- From: Phill Kelley <email@hidden>
- Date: Tue, 12 Aug 2003 11:41:15 +1000
At 21:18 -0400 11/08/2003, Mike Brinkman wrote:
>
I can't figure out why this method is causing my application to crash:
>
>
- (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];
You construct an object of type XObject here, and myObject points to it.
>
myObject = [myArray objectAtIndex:i];
You then overwrite myObject with whatever comes back from the array. The
object you allocated with the alloc/init in the preceding step is now
invisible (a memory leak).
>
>
// increment sum with floatValue of current object's amount
>
sum += [[myObject amount] floatValue];
>
>
// release myObject
>
[myObject release];
You release what came back from the array (as opposed to the thing you
created via alloc/init). The rules say you should only release what you
either create (via alloc/init etc) or explicitly retain. Releasing
something you don't own is what leads to the crash.
Assuming the information you want to work on is in the objects in your
array, I suggest removing both the "myObject = [[XObject alloc] init];" and
"[myObject release];" statements.
Regards, Phill
_______________________________________________
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.