Re: Crash with NSMutableArray and NSNumber
Re: Crash with NSMutableArray and NSNumber
- Subject: Re: Crash with NSMutableArray and NSNumber
- From: Philippe Mougin <email@hidden>
- Date: Thu, 9 Jan 2003 21:18:16 +0100
It seems that, in your displayScreen: method, the lines:
MyData * myData = [[MyData alloc] init] ;
NSNumber * number = [[NSNumber alloc] init] ;
... are useless. The data you want to use is already in _myArray when
the displayScreen: method is invoked (right?), so why creating new
data here?
The lines:
[number release] ;
[myData release] ;
... are probably causing the crash because they lead to unwanted
deallocation of some parts of your data. Try removing them.
Best,
Phil
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.