弧与Malloc:EXEC_BAD_ACCESS

发布于 2025-01-01 11:27:08 字数 840 浏览 4 评论 0原文

我已经从事一个项目有一段时间了,我决定跳到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2025-01-08 11:27:08

崩溃是因为您将 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 of malloc() 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 the realloc() then you may want to be using a temporary void* pointer that you then zero-fill manually before assigning to your object array.

深海不蓝 2025-01-08 11:27:08

malloc 函数不会将其分配的内存清零。内存可能包含随机垃圾。

来自 Clang 自动引用计数指南,第 4.2 节:

对于__strong对象,首先保留新的被指点;其次,左值加载了原始语义;第三,新的指针被存储到具有原始语义的左值中;最后,旧的被指点被释放

因此,这里可能发生的情况是 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:

For __strong objects, the new pointee is first retained; second, the lvalue is loaded with primitive semantics; third, the new pointee is stored into the lvalue with primitive semantics; and finally, the old pointee is released.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文