c++ - Access violation on a private array inside a class -
for part borrowed code rasterteks dx11 tutorial modified lightly or own use , taste. getting read access violation while using below inputclass set keystates. #include "inputclass.h" inputclass::inputclass() { } inputclass::inputclass(const inputclass& other) { } inputclass::~inputclass() { } void inputclass::initialize() { // initialize keys being released , not pressed. (int = 0; i<256; i++) { keystate[i] = false; } return; } void inputclass::keydown(unsigned int input) { // if key pressed save state in key array. keystate[input] = true; return; } void inputclass::keyup(unsigned int input) { // if key released clear state in key array. keystate[input] = false; return; } bool inputclass::iskeydown(unsigned int input) { // return state key in (pressed/not pressed). return keystate[input]; }
below main callback loop, 1 registered windowclass:
lresult callback wndproc(hwnd hwnd, uint message, wparam wparam, lparam lparam) { switch (message) { // message read when window closed case wm_destroy: { postquitmessage(0); return 0; } // check if window being closed. case wm_close: { postquitmessage(0); return 0; } default: { return applicationhandle->messagehandler(hwnd, message, wparam, lparam); } } }
finally, below secondary message handler part of systemclass:
lresult callback systemclass::messagehandler(hwnd hwnd, uint message, wparam wparam, lparam lparam) { switch (message) { case wm_keydown: { input->keydown((unsigned int)wparam); } case wm_keyup: { input->keyup((unsigned int)wparam); } default: { return defwindowproc(hwnd, message, wparam, lparam); } } }
the exception fired off when code gets switch/case/default list in secondary message handler. if comment these lines out program happily runs of course, no input.
any or clues invaluable. thank time.
try following modification of code:
void inputclass::keydown(unsigned int input) { if (input < 0 || input > 255) return; // if key pressed save state in key array. keystate[input] = true; return; } void inputclass::keyup(unsigned int input) { if (input < 0 || input > 255) return; // if key released clear state in key array. keystate[input] = false; return; }
(since haven't problems in constructor of inputclass
, not problem of private status of array)
Comments
Post a Comment