Re: EXC_BAD_ACCESS in NSData

2014-05-28 Thread Scott Ribe
On May 28, 2014, at 10:24 PM, Uli Kusterer wrote: > That said, if you want to be really endian-safe, use an XML file format saved > as UTF-8 like Property Lists. Most portable format there is. :-) Thanks a lot. I have to go rinse out my mouth now. -- Scott Ribe scott_r...@elevated-dev.com htt

Re: EXC_BAD_ACCESS in NSData

2014-05-28 Thread Charles Srstka
On May 28, 2014, at 11:24 PM, Uli Kusterer wrote: > Or change struct alignment or the size of ints or … I’ve been programming for > a couple of days, I’ve taken this into account. But I’d rather retroactively > go and fix something (you have to re-test when porting to a new platform > anyway)

Re: EXC_BAD_ACCESS in NSData

2014-05-28 Thread Charles Srstka
On May 27, 2014, at 3:51 PM, Greg Parker wrote: > On May 27, 2014, at 12:52 PM, Charles Srstka wrote: >> On May 27, 2014, at 2:08 PM, Greg Parker wrote: >>> On May 26, 2014, at 8:28 PM, Charles Srstka >>> wrote: On May 26, 2014, at 7:43 PM, Uli Kusterer wrote: > Regarding endi

Re: EXC_BAD_ACCESS in NSData

2014-05-28 Thread Uli Kusterer
On 26 May 2014, at 20:28, Charles Srstka wrote: > On May 26, 2014, at 7:43 PM, Uli Kusterer > wrote: >> Regarding endian-swapping, that depends on the file format. If you wrote >> that file yourself, you don’t usually need to do any swapping. > > That's true. For example, back in the PowerPC d

Re: EXC_BAD_ACCESS in NSData

2014-05-27 Thread Graham Cox
On 28 May 2014, at 5:52 am, Charles Srstka wrote: > a) 'bar' is a later addition to the file format, added some time after the > app was ported to Intel, and thus is in little-endian format whereas the > older parts of the file are in big-endian (and yes, I've seen formats that do > this), or

Re: EXC_BAD_ACCESS in NSData

2014-05-27 Thread Jens Alfke
On May 27, 2014, at 12:08 PM, Greg Parker wrote: > Untested code is incorrect code. Don't write endian-swapping code that you > can't test because you aren't using any other-endian platforms today. I disagree. There is a strong, longstanding convention for cross-platform binary data formats

Re: EXC_BAD_ACCESS in NSData

2014-05-27 Thread Greg Parker
On May 27, 2014, at 12:52 PM, Charles Srstka wrote: > On May 27, 2014, at 2:08 PM, Greg Parker wrote: >> On May 26, 2014, at 8:28 PM, Charles Srstka wrote: >>> On May 26, 2014, at 7:43 PM, Uli Kusterer >>> wrote: Regarding endian-swapping, that depends on the file format. If you wrote >>

Re: EXC_BAD_ACCESS in NSData

2014-05-27 Thread Charles Srstka
On May 27, 2014, at 2:08 PM, Greg Parker wrote: > On May 26, 2014, at 8:28 PM, Charles Srstka wrote: >> On May 26, 2014, at 7:43 PM, Uli Kusterer >> wrote: >>> Regarding endian-swapping, that depends on the file format. If you wrote >>> that file yourself, you don’t usually need to do any swa

Re: EXC_BAD_ACCESS in NSData

2014-05-27 Thread Greg Parker
On May 26, 2014, at 8:28 PM, Charles Srstka wrote: > On May 26, 2014, at 7:43 PM, Uli Kusterer > wrote: >> Regarding endian-swapping, that depends on the file format. If you wrote >> that file yourself, you don’t usually need to do any swapping. > > That's true. For example, back in the PowerP

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Charles Srstka
On May 26, 2014, at 7:43 PM, Uli Kusterer wrote: > Regarding endian-swapping, that depends on the file format. If you wrote that > file yourself, you don’t usually need to do any swapping. That's true. For example, back in the PowerPC days, we never had to endian-swap our file formats, because

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Graham Cox
On 27 May 2014, at 10:13 am, Jens Alfke wrote: > No, it’s extremely common to have a data format where a variable-length field > is prefixed with its length. That looks like what this code is trying to read. Alright, but you have to know that. I was looking at the code from a general standpoi

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Uli Kusterer
On 26 May 2014, at 16:20, Graham Cox wrote: > On 27 May 2014, at 12:54 am, Roland King wrote: >> datasize = *((unsigned int*)bytes); >> >> is a bit closer to what you might want but is endian-unaware. > > That's just as wrong - you are using the first few bytes of the data as the > length, w

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Jens Alfke
On May 26, 2014, at 4:20 PM, Graham Cox wrote: > That's just as wrong - you are using the first few bytes of the data as the > length, which it certainly isn't (except for possibly a very few special > cases that just so happen to have the length as the first field of the data). No, it’s extr

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Graham Cox
On 27 May 2014, at 12:54 am, Roland King wrote: > datasize = *((unsigned int*)bytes); > > is a bit closer to what you might want but is endian-unaware. That's just as wrong - you are using the first few bytes of the data as the length, which it certainly isn't (except for possibly a very fe

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Jens Alfke
On May 26, 2014, at 6:02 AM, Pax <45rpmli...@googlemail.com> wrote: > When this gets run, I get EXC_BAD_ACCESS code = 2 here. I'm demonstrably > reading less data than the file contains, and I've been able to read through > the file successfully up to this point. Please could someone suggest

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Scott Ribe
On May 26, 2014, at 10:06 AM, Pax <45rpmli...@googlemail.com> wrote: > As I say though, I think that the solution I posted is the better solution > anyway - since who wants a copy, with the additional overhead that a copy > entails, when you don't need to have one? Absolutely. (For read-only ac

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Pax
Shameful. I need more sleep! :-) I also need to reread my code more carefully before asking questions. As I say though, I think that the solution I posted is the better solution anyway - since who wants a copy, with the additional overhead that a copy entails, when you don't need to have on

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Scott Ribe
On May 26, 2014, at 9:32 AM, Uli Kusterer wrote: > Oh, and as Roland wrote, don’t forget to free() the pointer once you’re done > with it. But, just to be 100% clear, *not* the one you get from calling the bytes method ;-) -- Scott Ribe scott_r...@elevated-dev.com http://www.elevated-dev.com

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Roland King
Too true. That was a lot of bugs in 4 lines! > On 26 May, 2014, at 11:18 pm, Scott Ribe wrote: > >> On May 26, 2014, at 9:05 AM, Roland King wrote: >> >> The first one of those is ok... > > No, it's not--take a closer look ;-) > >> unsigned char* databuffer = (unsigned char*)malloc(datasize)

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Uli Kusterer
On 26 May 2014, at 06:44, Pax <45rpmli...@googlemail.com> wrote: > Apologies - the obvious important detail, and I missed it out entirely. I > have tried both of the following with the same result: > > unsigned char* databuffer = (unsigned char*)malloc(datasize); > [datastream getBytes:&data

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Scott Ribe
On May 26, 2014, at 9:05 AM, Roland King wrote: > The first one of those is ok... No, it's not--take a closer look ;-) > unsigned char* databuffer = (unsigned char*)malloc(datasize); > [datastream getBytes:&databuffer range:NSMakeRange(positionCounter, > datasize)]; -- Scott Ribe scott_r...

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Roland King
The first one of those is ok - as long as you remember to free databuffer later. The second is wrong, the argument to getBytes:range: has to be an allocated buffer of size at least the length of the range you pass in. That second one is just copying data to a random place in memory, probably zer

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Scott Ribe
On May 26, 2014, at 8:43 AM, Pax <45rpmli...@googlemail.com> wrote: > I should, I know, find out what was going wrong initially - but since I'd > rather not have a copy anyway, this really is the all-round better solution. What was wrong was that instead of passing the address of the buffer you

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Roland King
So you have unsigned char *bytes; and then datasize = (unsigned int)bytes; which sets datasize to the address of bytes cast to an unsigned integer which has nothing to do with the data in bytes at all and in some cases just truncates the address (OSX for instance) datasize = *((unsigned in

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Pax
Just to answer my own question, I find that: bytes=[[datastream subdataWithRange:NSMakeRange(positionCounter, datasize)]bytes]; Works perfectly. I should, I know, find out what was going wrong initially - but since I'd rather not have a copy anyway, this really is the all-round better solution

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Sandy McGuffog
Well, I find your code to be a bit confusing, but I’d say that both of those are wrong. I think that what you need is: unsigned char* databuffer = (unsigned char*)malloc(datasize); [datastream getBytes:databuffer range:NSMakeRange(positionCounter, datasize)]; // Packet content On May 26,

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Pax
Apologies - the obvious important detail, and I missed it out entirely. I have tried both of the following with the same result: unsigned char* databuffer = (unsigned char*)malloc(datasize); [datastream getBytes:&databuffer range:NSMakeRange(positionCounter, datasize)]; // Packet content

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Pax
Apologies - the obvious important detail, and I missed it out entirely. I have tried both of the following with the same result: unsigned char* databuffer = (unsigned char*)malloc(datasize); [datastream getBytes:&databuffer range:NSMakeRange(positionCounter, datasize)]; // Packet content

Re: EXC_BAD_ACCESS in NSData

2014-05-26 Thread Uli Kusterer
On 26 May 2014, at 06:02, Pax <45rpmli...@googlemail.com> wrote: > This is my snippet of code: >[datastream getBytes:&bytes > range:NSMakeRange(positionCounter, datasize)]; // Length of packet content >positionCounter+= datasize; What is bytes declared as,