i2c smbus filter function corrupting variables

2019-09-09 09:15发布

问题:

I have a simple function

#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; 
}

It takes in a value that was read from smbus and returns a value that was filtered with the last value it received. Here is its output from the printf statement

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

So what is happening? How is it that it still received the same input but all of a sudden went crazy? I don't set the fValue anywhere but this function.

I made these variables (y and yy) very internalized to the function because I was worried they were somehow being modified or collided with something else. But now that they are completely local I have no idea what is happening. I am using C++ classes so this should all be in its own space anyhow.

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?

回答1:

[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.



标签: c++ i2c