【问题标题】:64-bit build in OSX - inconsistant malloc errorOSX 中的 64 位构建 - 不一致的 malloc 错误
【发布时间】:2012-12-27 20:37:19
【问题描述】:

我在 Xcode 中遇到了一个非常不一致的错误:

malloc: *** error for object 0x1041146f8: incorrect checksum for freed object - object was probably modified after being freed. *** set a breakpoint in malloc_error_break to debug

我知道这不是我的直接代码,因为 32 位构建工作正常(架构设置为标准 32/64,仅构建活动架构设置为否)。它偶尔也会正常工作,我什至不更改评论,但只有大约 %10 的时间。

我使用断点跟踪错误,有时它发生在 ivar 上,例如:myClass = new MyClass,但有时它发生在删除不相关的 ivar 时。我尝试在创建新实例之前将 myClass 设置为 null 但这没有帮助,而且我很茫然,因为我不完全了解缓存、寄存器、堆和堆栈(这可能会深入了解原因这正在发生)。

这是我遇到错误的地方的一些代码。请注意,每组代码行是不同的位置和类,错误可能发生也可能不会发生。

错误 1

void functionA() {
    // bunch of unrelated code
    if (aAinterpFilter)
        delete aAinterpFilter;

    // this is where the first error sometimes happens
    aAinterpFilter = new InterpFilter((Window::Sinc::LP*)filterDesign, aAinterpFactor);
}

错误 2

Window::Sinc::LP::~LP ()
{
    // the z delete is where the error sometimes happens
    delete[] z;
    delete[] kernel;
}

错误 3

void AAOsc :: setPhase(double phase) {
    if (phase < 0.0) phase = 0.0;
    if (phase > 1.0) phase = 1.0;

    // this is where the error rarely happens, but it does sometimes.
    osc->tickInfo->curvPhase = static_cast<uint>(phase * 4294967296.0);
}

任何可能指向解决方案的想法将不胜感激。

GW

【问题讨论】:

  • 我会,但代码太复杂了,可能会让人更加困惑。如果您认为有必要,我可以这样做

标签: xcode macos 64-bit malloc 32bit-64bit


【解决方案1】:

您需要在建议的函数malloc_error_break() 上添加断点。运行应用程序并让调试器在该函数上中断。退后一两个堆栈帧,您将看到操作系统认为已释放您已修改的变量。然后,您需要弄清楚该变量之前可能在哪里被释放。

如果您有一块内存,您 malloced 然后写入,并且您不小心在malloc 返回的指针之前写入了几个字节,也会发生这种情况。你可以通过打开guard malloc 并让它再次发生来捕捉它。

【讨论】:

  • 我们发现了问题。我们在 z 变量上使用了 memset,它在 32 位中运行良好,但它清除了错误大小的内存块并破坏了 64 位块。
猜你喜欢
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2012-12-19
  • 2011-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多