Re: Can't understand what's causing NSRangeException
Re: Can't understand what's causing NSRangeException
- Subject: Re: Can't understand what's causing NSRangeException
- From: Bob Smith <email@hidden>
- Date: Wed, 7 May 2008 13:25:44 -0700
On May 7, 2008, at 8:56 AM, Vinayak Suley wrote:
Hi,
I'm new to objective-C so this might be elementary for some, but I
can't seem to figure out why this is happening.
Here's two code samples:
ONE:
SpectralData* spectrum = [selectedLightSource
spectralPowerDistribution];
NSMutableArray* lightSourceData1 = [spectrum data];
TWO:
NSMutableArray* lightSourceData2 = [[selectedLightSource
spectralPowerDistribution] data];
If my understanding of objective-C is correct, the second one is
just a short version of writing the first. But the first one works
fine and the second one causes an NSRangeException:
An uncaught exception was raised
*** -[NSCFArray objectAtIndex:]: index (-1877845435( or possibly
larger)) beyond bounds (41)
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[NSCFArray objectAtIndex:]: index (-1877845435( or
possibly larger)) beyond bounds (41)'
Both get properties, 'spectralPowerDistribution' and 'data' are
really simple. They just return the member variables of type
SpectralData* and NSMutableArray* respectively.
In my experience, when bizarre things like this happen the trouble
usually turns out to be bad memory management, i.e. not retaining an
object, or over-releasing an object. Those two bits of code are
logically identical, but they are implemented differently by the
compiler. That makes no difference if everything else in your
application is correct, however it can easily change the failure mode
if you have memory management problems.
If you are new to Cocoa and haven't studied these already, definitely
read (and then read again) the following:
http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/
MemoryMgmt.html
http://developer.apple.com/documentation/Cocoa/Conceptual/
GarbageCollection/Introduction.html
Bob
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden