iOSPawan
iOSPawan

Reputation: 2894

getting crash om memory set After updating to lion and xCode

I have just updated to Lion and now my app is crashing which was working fine in older version. It crash on memset function with no logs.

unsigned char *theValue;
add(theValue, someotherValues);

I have passed theValue reference to function

add(unsigned char *inValue, some other perameter) {
memset(inValue,0,sizeOf(inValue)); // **here it is crashing**
}

Upvotes: 0

Views: 113

Answers (3)

Marcelo Cantos
Marcelo Cantos

Reputation: 185962

Is there really no code between the declaration of theValue and the call to add()? If so, then that's your problem. You are passing a random value as the first parameter to memset().

For this code to make sense, you have to allocate a block of memory for theValue and pass its size to add(), like so:

unsigned char *theValue = new unsigned char[BUFSIZE]; // Or malloc
add(theValue, BUFSIZE, ...);

void add(unsigned char *inValue, size_t bufsize, ...) {
    memset(inValue, 0, bufsize);
    ...
}

Upvotes: 2

deanWombourne
deanWombourne

Reputation: 38475

unsigned char *theValue;

This points to a random bit of memory (or 0). Until you call malloc you don't own what it's pointing at so you can't really memset it.

Upvotes: 1

Nekto
Nekto

Reputation: 17877

Do you allocate memory for inValue?

1)

add(unsigned char *inValue, some other perameter) {
    inValue = (unsigned char*)malloc(sizeof(inValue));
    memset(inValue,0,sizeOf(inValue)); // **here it is crashing**
}

2)

theValue = (unsigned char*)malloc(sizeof(inValue));
add(theValue, ...)

Upvotes: 1

Related Questions