Re: problem encoding large float* matrix
Re: problem encoding large float* matrix
- Subject: Re: problem encoding large float* matrix
- From: Nick Zitzmann <email@hidden>
- Date: Tue, 1 Dec 2009 11:57:47 -0700
On Dec 1, 2009, at 11:47 AM, James Maxwell wrote:
> NSUInteger coincsSize = maxCoincs * inputSize * sizeof(float);
> NSData* coincData = [NSData dataWithBytesNoCopy:&coincidences length:coincsSize];
> [aCoder encodeBytes:[coincData bytes] length:coincsSize forKey:@"coincidences"];
>
> The app runs fine, but when I try to save, boom... EXC_BAD_ACCESS.
>
> Is this a decent way to save a float* array?
Are you really sure you want to use -dataWithBytesNoCopy:? That method makes the data object take ownership of the bytes, so when the data is freed, your malloc'd pointer goes away, and accessing it afterwards will cause a crash. So unless you really know what you're doing, you ought to use -dataWithBytes: instead.
Also, you might want to consider just encoding the NSData object unless you have a really good reason for doing what you're doing. Also, if your file format is portable across architectures, don't forget to store & retrieve the floats in a consistent format (meaning big-endian or little-endian).
Nick Zitzmann
<http://www.chronosnet.com/>
_______________________________________________
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