Re: Crash with NSMutableArray and NSNumber
Re: Crash with NSMutableArray and NSNumber
- Subject: Re: Crash with NSMutableArray and NSNumber
- From: Chris Giordano <email@hidden>
- Date: Thu, 9 Jan 2003 15:19:32 -0500
Andrew,
The problem you're running into has to do with your handling of the
NSNumber and MyData object in the displayScreen method. It seems to me
that you're treating them as scalars (i.e., as ints) rather than as
objects.
For example, first, you create an NSNumber instance, number:
NSNumber * number = [[NSNumber alloc] init] ;
However, a few lines later, you leak the original number you created,
and set number to a new instance that you get from the myData object:
number = [myData getNumber ] ;
Later, still, you release this new object.
[number release] ;
What you're running into is the fact that this last release shouldn't
be happening. You're getting the number instance from myData, but you
aren't retaining or copying it, so the retain count isn't increasing.
By releasing it later, the object is being deallocated and then likely
the memory that it occupied is being used for something else, causing
further attempts to access number to give you errors. You're also
doing the same thing with the myData object. The first time through,
the objects exist, but at the end of the method, your objects are
released and then deallocated, so the next time the code is executed,
you're grabbing invalid data and getting errors.
You can probably fix your problem by just removing the release
statements from displayScreen:. You should also change the lines where
you create number and myData to simply declare them so you don't leak
these objects each time this method is called:
MyData * myData = nil;
NSNumber * number = nil;
Doing this should remove your problem.
Hope this helps.
chris
On Thursday, January 9, 2003, at 11:17 AM, <email@hidden>
wrote:
I have a problem with a current program. The layout is as follows.
The main data source is held in a NSMutableArray. The source is a
group of custom NSObjects (MyData) which are used to display
information on the screen.
The NSMutableArray is declared with;
_myArray = [[NSMutableArray alloc] init] ;
When I wish to refresh the screen a call an instance called Render
using the following;
Render * render = [[Render alloc] init] ;
[render displayScreen: self] ;
[render release] ;
Render then looks something like this;
- (void)displayScreen: (id)sender
{
int numberOfLines, x ;
int originalNumber ;
MyData * myData = [[MyData alloc] init] ;
NSNumber * number = [[NSNumber alloc] init] ;
.
numberOfLines = [sender getNumberOfLines] ;
.
for( x = 0; x < numberOfLines; x++ )
{
myData = [sender getLine: x] ;
number = [myData getNumber ] ;
originalNumber = [number intValue] ;
.
}
[number release] ;
[myData release] ;
}
to return the value for myData and numberOfLines the following are
used;
- (int)getNumberOfLines
{
return [_myArray count] ;
}
- (MyData *)getLine: (int)index
{
return [_myArray objectAtIndex: index] ;
}
similarly within MyData I have the following
@interface MyData : NSObject
{
NSNumber * aNumber ;
NSString * aString ;
NSNumber * anotherNumber ;
.
}
to recover aNumber
- (NSNumber *)getNumber
{
return aNumber ;
}
My problem is as follows, the first time I run the redraw routine
everything works as expected, but the second time I run it, any calls
to returned NSNumber items return a EXC_BAD_ACCESS error in the
debugger, so the line originalNumber = [number intValue] causes a
crash. However similar calls to returned NSString items are not
affected.
Stepping through the code at runtime reveals that the all of the
values for the NSString items remain constant, however the isa
reference number changes from one execution to the next on the
NSNumber items.
Is this the cause of my crash? Or what suggestions can you offer?
Thanks,
Andrew...
_______________________________________________
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.