0

I cannot for the life of me figure out this error Access violation reading location 0x00000088, I am programming a script that controls the leds on my keyboard, and this error pops up after only about 5 keystrokes.

Full error is

Exception thrown at 0x10013871 (msvcp140d.dll) in TacoKeyBoard2.exe: 0xC0000005: Access violation reading location 0x00000088.

main.cpp

#include "KEYBOARD.h"

// Define Functions
void CALLBACK KeyCallBack(int iRow, int iCol, bool press);
int main_logic();

// Define Variables (Globals)
KEY_MASTER master;
vector<KEY_LIGHT> lights;
mutex mylock;

int main()
{
    // INIT VALUES
    srand(time(0));
    bool RUN = true;

    // Initialize Control Device
    SetControlDevice(DEV_MKeys_S);
    EnableLedControl(true);

    // Set The Callback
    SetKeyCallBack(KeyCallBack);
    EnableKeyInterrupt(true);

    // RESET THE MASTER OF THE KEYBOARD
    master = KEY_MASTER(KEY_COLOR(255, 0, 0));

    // INITIALIZE THREAD FOR LOGIC
    thread logic(main_logic);

    while (RUN)
    {
        //KEY_COLOR c = master.GetColor();
        //cout << (int)c.r << ":" << (int)c.g << ":" << (int)c.b << endl;
        SetAllLedColor(master.getColors());
        Sleep(1); // Make it sleep for however long
    }
    logic.join();

    return 0;
}

int main_logic()
{
    // INIT VALUES
    double RUN = true;

    while (RUN)
    {
        int i = 0;
        while (i < lights.size() && lights.size() > 0)
        {
            mylock.lock();
            bool remove = lights[i].update(0.1);
            if (remove)
            {
                lights.erase(lights.begin() + i);
            }
            else
            {
                i++;
            }
            mylock.unlock();
        }
        //cout << "Thread 2" << endl;
        Sleep(50); // Make it sleep for however long
    }
    return 0;
}

void CALLBACK KeyCallBack(int iRow, int iCol, bool bPressed)
{

    if (bPressed)
    {
        mylock.lock();
        KEY_LIGHT a = KEY_LIGHT(iCol, iRow, master.GetColor());
        lights.push_back(a);
        mylock.unlock();
    }
}

Notice the keystrokes are received by a callback function provided by the sdk.

keyboard.h

#ifndef KEYBOARD_H
#define KEYBOARD_H

#include "Functions.h"
#include <iostream>
#include <vector>
#include <time.h>
#include <mutex>
#include <thread>
#include "SDKDLL.h"

using namespace std;

const KEY_COLOR _colors[30] = {
    KEY_COLOR(255,0,0),
    KEY_COLOR(255,51,0),
    KEY_COLOR(255,102,0),
    KEY_COLOR(255,153,0),
    KEY_COLOR(255,204,0),
    KEY_COLOR(255,255,0),
    KEY_COLOR(204,255,0),
    KEY_COLOR(153,255,0),
    KEY_COLOR(102,255,0),
    KEY_COLOR(51,255,0),
    KEY_COLOR(0,255,0),
    KEY_COLOR(0,255,51),
    KEY_COLOR(0,255,102),
    KEY_COLOR(0,255,153),
    KEY_COLOR(0,255,204),
    KEY_COLOR(0,255,255),
    KEY_COLOR(0,204,255),
    KEY_COLOR(0,153,255),
    KEY_COLOR(0,102,255),
    KEY_COLOR(0,51,255),
    KEY_COLOR(0,0,255),
    KEY_COLOR(51,0,255),
    KEY_COLOR(102,0,255),
    KEY_COLOR(153,0,255),
    KEY_COLOR(204,0,255),
    KEY_COLOR(255,0,255),
    KEY_COLOR(255,0,204),
    KEY_COLOR(255,0,153),
    KEY_COLOR(255,0,102),
    KEY_COLOR(255,0,51),
};

class KEY_LIGHT {
private:
    int x, y;
    double step;
    KEY_COLOR col;
public:
    bool update(double n);
    KEY_LIGHT(int _x, int _y, KEY_COLOR _c);
};

class KEY_MASTER {
private:
    COLOR_MATRIX colors; // Store the key colors
    int _c;
    //vector<KEY_LIGHT> lights; // Store the key press light objects
public:
    COLOR_MATRIX getColors() { return colors; } // Get the colors
    void SetKeyColor(int x, int y, KEY_COLOR c);
    KEY_MASTER(KEY_COLOR c); // Init inside main
    KEY_MASTER(); // Init as a global
    KEY_COLOR GetColor();
};

extern KEY_MASTER master;


#endif

keyboard.cpp

    #include "KEYBOARD.h"
#include "Functions.h"

// FUNCTIONS

KEY_MASTER::KEY_MASTER(KEY_COLOR c)
{
    SetFullLedColor(c.r, c.g, c.b);
    colors.KeyColor[MAX_LED_ROW][MAX_LED_COLUMN] = { KEY_COLOR(c.r,c.g,c.b) };
    _c = 0;
}

KEY_MASTER::KEY_MASTER()
{
    cout << "MASTER HAS BEEN MADE\n";
}

void KEY_MASTER::SetKeyColor(int _x, int _y, KEY_COLOR _col)
{
    colors.KeyColor[_y][_x] = _col;
}

KEY_COLOR KEY_MASTER::GetColor()
{
    KEY_COLOR col = _colors[_c];
    _c++;
    if (_c >= 30)
        _c = 0;
    return col;
}

bool KEY_LIGHT::update(double n)
{
    step += n;
    KEY_COLOR temp = col;
    if (step < 2.4)
    {
        double a = Clamp<double>(step, 0, 1), b = Clamp<double>(step - 0.7, 0, 1), c = Clamp<double>(step - 1.4, 0, 1);
        //if (a != 1)
        //{
            temp.r = temp.r*a;
            temp.g = temp.g*a;
            temp.b = temp.b*a;
            master.SetKeyColor(x, y, temp);
            master.SetKeyColor(x+1, y, temp);
            master.SetKeyColor(x-1, y, temp);
            master.SetKeyColor(x, y+1, temp);
            master.SetKeyColor(x, y-1, temp);
        //}
        //if (b > 0 && b != 1)
        //{
            temp.r = temp.r*b;
            temp.g = temp.g*b;
            temp.b = temp.b*b;
            master.SetKeyColor(x-1, y-1, temp);
            master.SetKeyColor(x-1, y+1, temp);
            master.SetKeyColor(x+1, y-1, temp);
            master.SetKeyColor(x+1, y+1, temp);
        //}
        //if ( c > 0)
        //{
            temp.r = temp.r*c;
            temp.g = temp.g*c;
            temp.b = temp.b*c;
            master.SetKeyColor(x-2, y, temp);
            master.SetKeyColor(x-2, y+1, temp);
            master.SetKeyColor(x-2, y-1, temp);

            master.SetKeyColor(x+2, y, temp);
            master.SetKeyColor(x+2, y+1, temp);
            master.SetKeyColor(x+2, y-1, temp);

            master.SetKeyColor(x, y-2, temp);
            master.SetKeyColor(x+1, y-2, temp);
            master.SetKeyColor(x-1, y-2, temp);

            master.SetKeyColor(x, y+2, temp);
            master.SetKeyColor(x+1, y+2, temp);
            master.SetKeyColor(x-1, y+2, temp);
        //}
            return false;
    }
    return true;
}

KEY_LIGHT::KEY_LIGHT(int _x, int _y, KEY_COLOR _c)
{
    step = 0;
    x = _x;
    y = _y;
    col = _c;
}

any help would be greatly appreciated, I've tried debugging this, but could not get the error to stop without removing the callback, I have also looked up the error and found nothing for the error Access violation reading location 0x00000088

I am building this on windows with visual studio and building for x86

  • 1
    http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Biffen Jan 30 '17 at 10:36
  • Related: http://stackoverflow.com/questions/8628951/remove-elements-of-a-vector-inside-the-loop & http://stackoverflow.com/questions/8597240/how-to-delete-an-element-from-a-vector-while-looping-over-it – Mailerdaimon Jan 30 '17 at 10:37
  • 1
    Just to help you with interpreting the error, 0x00000088 is an invalid address. You can't effectively google it because it's caused by your program. By far the most likely cause is using a null pointer and adding a member offset of +0x88. – MSalters Jan 30 '17 at 10:37
  • 2
    `double RUN = true;` looks odd to me. – mch Jan 30 '17 at 10:38
  • 1
    Replace `bool remove = lights[i].update(0.1);` with this: `bool remove = lights.at(i).update(0.1);`. If you did that, do you get an `out_of_range` exception thrown? Also, `mylock.lock()...mylock.unlock()` is not safe, as what you're doing between those calls may throw an exception, causing the mutex to stay locked. Consider using RAII techniques. – PaulMcKenzie Jan 30 '17 at 10:39
  • `I tried doing lights.at(i).update(0.1)`, I get the exact same error, I ahve yet to try the RAII thing out, but I need to get some sleep so I will probably continue looking into this tomorrow. – YOUR WORST TACO Jan 30 '17 at 10:51
  • 1
    "I am building this on windows with visual studio..." - then you have arguably the best user-mode debugger on earth for the Windows platform at your disposal, and you should use it accordingly. As presented, there isn't much we can do besides wild guesses, and that isn't what this site is about. There are a ton of things either suspicious, unorthodox, or just plain wtf in this code. It needs serious debugging, probably back when there was only a third this much code. – WhozCraig Jan 30 '17 at 11:02
  • I've been tearing this apart and I think I've narrowed it down to update, I think what may be the issue is I try to change something inside an array that is outside of the array bounds. I'm going to look into this further once I'm home again. – YOUR WORST TACO Jan 30 '17 at 16:11
  • @YOURWORSTTACO *I think what may be the issue is I try to change something inside an array that is outside of the array bounds* -- If that's the case, then consider using `std::array` instead of vanilla C++ arrays. You can use `at()` with `std::array` to comfirm if your indices are within bounds. Example: `const std::array colors = { {KEY_COLOR(255,0,0), KEY_COLOR(255,51,0),,...} };` – PaulMcKenzie Jan 30 '17 at 16:20
  • Thanks, I will definitely look into that, although I may be completely wrong, I've only been able to narrow down the error to the update function, I will be doing more conclusive debugging in that area once I get home though. – YOUR WORST TACO Jan 30 '17 at 16:46
  • Alright, I figured it out, It was indeed the update function, because that was making a call to the function SetKeyColor which sets a key color at the given indexes. What I did is I used my Clamp function to clamp the value's passed through to 0 and the MAX_ROW and MAX_COL. The error was derived from the lock not being unlocked once this happened. – YOUR WORST TACO Jan 30 '17 at 21:26

0 Answers0