• 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: Buddy Kurz <email@hidden>
  • Date: Mon, 11 Aug 2003 21:11:44 -0700

I am thinking that the big piece of the puzzle you are missing is that you don't assign values or contents to objects using = (assignment operator)
I've seen many posts from people trying to do this with strings.
You must use some sort of set method [myObject setValue:somethingMatchingTheExpectedType] to accomplish this.
In your code,
When you do myObject=[[XObject alloc]init] you create an object and then assign its address to the variable myObject.
In the next line you change the same variable (pointer) to the address of an object in myArray (the one at (int)object at index)
At this point you have lost the pointer to the new (empty) object and caused a memory leak.
After your computation you release the object pointed to by myObject. This is now pointing to the object in myArray. The object in myArray wasn't retained anywhere else (except by myArray) so it gets released and its memory gets reused. You haven't notified myArray of any change to the status of objectAtIndex so it now points to a bogus memory location.
The next time you access that array item you get an error.

the following is much simpler and may actually work

for (i = 0; i < [myArray count]; i++)
{
// increment sum with floatValue of current object's amount
sum += [[myArray objectAtIndex:i] floatValue];

}

bk

On Monday, August 11, 2003, at 06:18 PM, 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];
myObject = [myArray objectAtIndex:i];

// increment sum with floatValue of current object's amount
sum += [[myObject amount] floatValue];

// release myObject
[myObject release];
}

[sumField setFloatValue:sum];
}

All this method is supposed to do is sum the values from an NSNumber object
in an NSMuatableArray, and place the value in a field. XObject is a subclass
of NSObject. XObject releases all of its objects in its dealloc method and
calls [super dealloc].

updateSum gets called when I add another object to myArray. It works fine
with the [myObject release] method commented out, but I get a signal 10
(SIGBUS) error if it's there and I try to add another object to myArray. If
I try [myObject autorelease] I get a signal 11 (SIGSEGV) error.

Does myObject need to be released since it's a local instance of XObject?
I'm trying to be really careful about memory leaks.
_______________________________________________
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.
_______________________________________________
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: Questions about retain count / NSOpenPanel behaviour
  • Next by Date: Re: Coding object version with non-keyed (un)archiver
  • Previous by thread: Re: SIGBUS/SIGSEGV error problems
  • Next by thread: Re: SIGBUS/SIGSEGV error problems
  • Index(es):
    • Date
    • Thread