开发者

getting crash om memory set After updating to lion and xCode

开发者 https://www.devze.com 2023-04-11 10:06 出处:网络
I have just updated to Lion and now my app is crashing which was working fine 开发者_Go百科in older version. It crash on memset function with no logs.

I have just updated to Lion and now my app is crashing which was working fine 开发者_Go百科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**
}


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);
    ...
}


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, ...)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消