0

我有一个简单的功能

#define AMB_FILTER 0.7f

int32_t fValue; (this is declared in the class header)

int32_t Ambient::filter(uint32_t raw)
{
    // If we have no preliminary fValue we don't need to calculate a filter
    if(fValue == -1)
    {
        fValue = raw;
        return fValue;
    }

    float y, yy;

    y = (1.0f - AMB_FILTER) * (float) raw;
    yy = AMB_FILTER * (float) fValue;

    fValue =  (int32_t) (y + yy);

    printf("filter raw %d y %f yy %f fValue %d\n",raw, y, yy, fValue);

    return fValue; 
}

它接受一个从smbus读取的值,并返回一个用它收到的最后一个值过滤的值。这是 printf 语句的输出

filter raw 454 y 136.200012 yy 317.799988 fValue 454
filter raw 454 y 136.200012 yy 317.799988 fValue 454
filter raw 454 y 136.200012 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy 317.799988 fValue 454
filter raw 454 y 136.200012 yy 317.799988 fValue 454
filter raw 455 y 136.500000 yy -731751040.000000 fValue -731750912
filter raw 455 y 136.500000 yy -512225632.000000 fValue -512225504
filter raw 455 y 136.500000 yy -358557856.000000 fValue -358557728
filter raw 455 y 136.500000 yy -250990400.000000 fValue -250990256
filter raw 454 y 136.200012 yy -175693184.000000 fValue -175693040

那么发生了什么?为什么它仍然收到相同的输入,但突然变得疯狂?除了这个函数,我没有在任何地方设置 fValue。

我使这些变量(y 和 yy)非常内化到函数中,因为我担心它们会以某种方式被修改或与其他东西发生冲突。但现在他们完全是本地的,我不知道发生了什么。我正在使用 C++ 类,所以无论如何这都应该在它自己的空间中。

EDIT: In fact if I let it run a little longer the variables I keep for the different i2c Chip addresses also become corrupted to -1075766188. What the hell?

4

1 回答 1

2
[t1] filter raw 454 y 136.200012 yy 317.799988 fValue 454
[t2] filter raw 455 y 136.500000 yy -731751040.000000 fValue -731750912

Sometime between t1 and t2 the value of fValue was corrupted. The corruption of yy is a consequence of fValue's corruption. Look elsewhere in your program for the culprit.

  • If you can't use valgrind, then sprinkle sanity checks on the valueof fvalue throughout your program.
  • Print the value of this in your routine. See if it becomes unusual.
  • Print the value of other member variables from Ambient. See if they become unusual.
  • Try commenting out huge tracts of code. Start with 1/2 of your program. If the bug is still there, comment out 1/2 of the remainder. Continue until you find a single line that controls the corruption.

As to how it was corrupted, there are many possibilities. Perhaps you dereference a wild pointer somewhere. Perhaps you write beyond the end of an array. Perhaps you are operating on a previously destroyed Ambient.

于 2012-11-09T15:44:05.300 回答