弧与Malloc:EXEC_BAD_ACCESS
我已经从事一个项目有一段时间了,我决定跳到 ARC。 我遇到过一些代码每次都会崩溃,我想知道为什么。我设法将其简化为以下片段:
typedef __strong id MYID;
int main(int argc, char *argv[])
{
MYID *arr = (MYID *) malloc(sizeof(MYID) * 4);
arr[0] = @"A"; // always get an EXEC_BAD ACCESS HERE
arr[1] = @"Test";
arr[2] = @"Array";
arr[3] = @"For";
// uh oh, we need more memory
MYID *tmpArray = (MYID *) realloc(arr, sizeof(MYID) * 8);
assert(tmpArray != NULL);
arr = tmpArray;
arr[4] = @"StackOverflow"; // in my actual project, the EXEC_BAD_ACCESS occurs here
arr[5] = @"Is";
arr[6] = @"This";
arr[7] = @"Working?";
for (int i = 0; i < 8; i++) {
NSLog(@"%@", arr[i]);
}
return 0;
}
我不太确定这里发生了什么,在 4 个不同的项目中对此感到厌倦,但它们都失败了。我的 malloc
调用有问题吗?有时它返回 null,有时它返回一个我无法访问的指针。
I have been working on a project for some time now, and I decided to make the jump to ARC.
I came across some code that was bombing out every time, and I would like to know why. I have managed to simplify it down to this snippet:
typedef __strong id MYID;
int main(int argc, char *argv[])
{
MYID *arr = (MYID *) malloc(sizeof(MYID) * 4);
arr[0] = @"A"; // always get an EXEC_BAD ACCESS HERE
arr[1] = @"Test";
arr[2] = @"Array";
arr[3] = @"For";
// uh oh, we need more memory
MYID *tmpArray = (MYID *) realloc(arr, sizeof(MYID) * 8);
assert(tmpArray != NULL);
arr = tmpArray;
arr[4] = @"StackOverflow"; // in my actual project, the EXEC_BAD_ACCESS occurs here
arr[5] = @"Is";
arr[6] = @"This";
arr[7] = @"Working?";
for (int i = 0; i < 8; i++) {
NSLog(@"%@", arr[i]);
}
return 0;
}
I'm not quite sure what is happening here, tired this in 4 different projects, and they all fail. Is there something wrong with my malloc
call? Sometimes it returns null, and other times it returns a pointer that I can't access.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
崩溃是因为您将 malloc 内存转换为 C 对象数组。当您尝试分配给其中一个插槽时,ARC 将释放先前的值,该值将成为垃圾内存。尝试使用
calloc()
而不是malloc()
来获取归零内存,它应该可以工作。请注意,您的
realloc()
调用也不会对分配的任何新内存进行零填充,因此,如果您需要realloc()
那么您可能需要使用临时内存void*
指针,然后在分配给对象数组之前手动将其填零。The crash is because you're casting malloc'd memory to a C array of objects. The moment you try to assign to one of the slots, ARC will release the previous value, which will be garbage memory. Try using
calloc()
instead ofmalloc()
to get zeroed memory and it should work.Note that your
realloc()
call will also not zero-fill any new memory that's allocated, so if you need therealloc()
then you may want to be using a temporaryvoid*
pointer that you then zero-fill manually before assigning to your object array.malloc
函数不会将其分配的内存清零。内存可能包含随机垃圾。来自 Clang 自动引用计数指南,第 4.2 节:
因此,这里可能发生的情况是
malloc
返回包含随机非零值的内存。 ARC 尝试使用该随机值作为指向对象的指针并释放它,但它不是有效的对象指针。碰撞。The
malloc
function does not zero the memory it allocates. The memory can contain random garbage.From the Clang Automatic Reference Counting guide, section 4.2:
So what's probably happening here is
malloc
is returning memory that contains random non-zero values. ARC tries to use that random value as a pointer to an object and release it, but it's not a valid object pointer. Crash.