In my iPhone application, I have the following line in a constructor:
self.myVar = myVar_in;
where myVar is a property and myVar_in is a parameter passed to the constructor.
When I run the code, I get an EXC_BAD_ACCESS
error on this line. However, when I replace the line with:
[myVar release];
[myVar_in retain];
myVar = myVar_in;
the code runs fine. My property is declared like this: NSNumber *myVar; ... @property (retain) NSNumber *myVar;
The error is consistent and I'm positive it's not a variable scope issue. Can someone explain this behavior?
EDIT: I've confirmed that myVar_in is valid right before the line(s) are executed. Here's the actual code, although it won't help much:
-(GetAddressRequestHelper*)initWithRequest:(ClientRequest*)request delegate:(id<ServerResponseDelegate>)delegate number:(NSNumber*)myVar_in location:(CLLocation*)location {
self = [super initWithRequest:request delegate:delegate];
if( self ) {
// same behavior even if this line is uncommented!!!
myVar_in = [NSNumber numberWithInt:123];
// prints "myVar_in is 123"
NSLog(@"myVar_in is %@",myVar_in);
// doesn't throw exception
/*[myVar release];
[myVar_in retain];
myVar = myVar_in;*/
// throws exception
self.myVar = myVar_in;
self.location=location;
}
return self;
}
EDIT2: I've found I still get the behavior when I explicitly initialize the param with myVar_in = [NSNumber numberWithInt:123];
!
Thanks