在Xcode中打开一些调试选项后,我得到以下输出:

GuardMalloc[Roadcast-4010]: free: magic is 0x0000090b, not 0xdeadbeef.
GuardMalloc[Roadcast-4010]: free: header magic value at 0x43f49bf0, for block 0x43f49c00-0x43f50000, has been trashed by a buffer underrun.
GuardMalloc[Roadcast-4010]: Try running with MALLOC_PROTECT_BEFORE to catch this error immediately as it happens.

如何开启MALLOC_PROTECT_BEFORE

UPDATE :

Mac Developer Library > Guard_Malloc做了什么,记录在ojit_a中:

最佳答案

要在Xcode中启用MALLOC_PROTECT_BEFORE,请在Xcode菜单中转到

产品>方案>编辑方案...

然后在弹出的页面中,转到“参数”,然后在“环境变量”下,添加MALLOC_PROTECT_BEFORE并为其赋予值1。您可以在屏幕截图1中看到这一点:

要使其实际使用,现在单击“诊断”并勾选“Enable Guard Malloc”,如Scrrenshot 2所示:

然后单击“确定”完成。

要测试检测缓冲区不足是否现在可以正常工作,请使用以下代码:

    int* allocated = malloc(1024);

    printf("If this is the last line printed, then MALLOC_PROTECT_BEFORE is enabled and buffer underruns are detected.\n");

    int value = allocated[-5]; // fails if MALLOC_PROTECT_BEFORE is set AND activated

    printf("The value read from unallocated memory space is %i.\n", value);
    printf("If this line is printed, then MALLOC_PROTECT_BEFORE is NOT enabled and buffer underruns can occur unnoticed.\n");

    free(allocated);
    allocated = NULL;

关于ios - 如何在Xcode中启用MALLOC_PROTECT_BEFORE?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24059539/

10-10 17:27