Access violation reading location 0x00000088 C++

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

c++
runtime-error
asked on Stack Overflow Jan 30, 2017 by YOUR WORST TACO

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0