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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to