• 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: 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.

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

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