Ios 如何在Xcode中启用MALLOC_PROTECT_?

Ios 如何在Xcode中启用MALLOC_PROTECT_?,ios,xcode,simulator,Ios,Xcode,Simulator,在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[R

在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\u PROTECT\u

更新

MALLOC\u PROTECT\u之前所做的工作记录在:

libgmalloc的行为可以通过几个额外的步骤来改变 环境变量:

马洛克保护

如果设置了此标志,则libgmalloc会更努力地检测缓冲区 跑垒。具体来说,libgmalloc放置分配的 缓冲区位于虚拟内存页的开头,然后保护该页 之前缓冲区不足会导致错误。无条件的行为 此变量集用于将缓冲区的结尾放置在 最后一页的分配,并保护后一页


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

产品>方案>编辑方案

然后在弹出的页面中,转到Arguments(参数)并在Environment Variables(环境变量)下,在
之前添加
MALLOC\u PROTECT\u并为其指定值
1
。您可以在屏幕截图1中看到:

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

然后单击OK,就完成了

要测试检测缓冲区不足现在是否有效,请使用以下代码:

    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;