2

我在 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

4

1 回答 1

0

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

如果您有一块您malloc编辑然后写入的内存并且您不小心在返回的指针之前malloc写入了几个字节,这也可能发生。您可以通过打开警卫 malloc并使其再次发生来抓住它。

于 2013-01-14T05:19:01.963 回答