Re: EXC_BAD_ACCESS in NSData
Re: EXC_BAD_ACCESS in NSData
- Subject: Re: EXC_BAD_ACCESS in NSData
- From: Uli Kusterer <email@hidden>
- Date: Mon, 26 May 2014 08:32:56 -0700
On 26 May 2014, at 06:44, Pax <email@hidden> 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:&databuffer range:NSMakeRange(positionCounter, datasize)]; // Packet content
As Scott says, you’re passing the address of a variable containing the address of the allocated memory, instead of just giving it the address of the allocated memory as you should. It’s like being asked to enter your address into a form, and instead you give them the address of a friend where they’re supposed to ask for your address. Since they don’t expect that, they will just deliver your order to your friend. No surprise it doesn’t work.
> and
>
> unsigned char* bytes;
> [datastream getBytes:&bytes range:NSMakeRange(positionCounter, datasize)]; // Packet content
>
> Either way, I get the same result.
This makes no sense. You’re still giving the address of a variable that can contain an address (which is wrong), but even if you didn’t: Since you never actually write an address into ‘bytes’ you get a random value (whatever happened to be in that location in memory at the time).
You may want to read some more about memory, pointers etc. (like the parts I wrote in my ‘Masters of the Void’ C tutorial: http://masters-of-the-void.com/book5.htm) to make sure you have a correct understanding. Something still seems to be fuzzy in your head.
Oh, and as Roland wrote, don’t forget to free() the pointer once you’re done with it.
Cheers,
-- Uli Kusterer
“The Witnesses of TeachText are everywhere...”
http://zathras.de
_______________________________________________
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