queue push crashes
I am getting this message on queue push:
Access violation reading location 0xffffffffffffffff
|
I don't understand why, so i don't know how to fix it.
This is the minimal code that reproduces the problem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
|
#include <iostream>
#include <vector>
#include <queue>
#include <conio.h>
enum EEventType
{
EET_BUTTON_PRESSED = 0
};
struct Message
{
int id;
EEventType msg;
};
class IEventHandler
{
public:
virtual ~IEventHandler() {}
virtual void onEvent(const Message& msg) = 0;
};
class GUIManager;
class GUIElement
{
protected:
GUIManager* mgr;
int id;
public:
GUIElement(GUIManager* _mgr, int _id) : id(_id) {}
virtual ~GUIElement() {}
virtual void onMouseUp() {}
};
class GUIButton : public GUIElement
{
public:
GUIButton(GUIManager* _mgr, int _id) : GUIElement(_mgr, _id) {}
void onMouseUp();
};
class GUIManager
{
private:
IEventHandler* eventHandler;
std::queue<Message> qMsg;
std::vector<GUIElement*> vElements;
public:
GUIManager(IEventHandler* eh)
: eventHandler(eh)
{
}
~GUIManager()
{
for(std::size_t i = 0; i < vElements.size(); ++i)
{
delete vElements[i];
}
vElements.clear();
}
void addMessage(Message msg)
{
qMsg.push(msg);
}
GUIButton* addButton(int _id)
{
GUIButton* bttn = new GUIButton(this, _id);
vElements.push_back(bttn);
return bttn;
}
void update()
{
if(eventHandler)
{
while(!qMsg.empty())
{
eventHandler->onEvent(qMsg.front());
qMsg.pop();
}
}
}
};
void GUIButton::onMouseUp()
{
Message m;
m.id = id;
m.msg = EET_BUTTON_PRESSED;
mgr->addMessage(m);
}
class MyEventHandler : public IEventHandler
{
public:
void onEvent(const Message& msg)
{
std::cout << "Button: " << msg.id << " was pressed!" << std::endl;
}
};
int main()
{
MyEventHandler meh;
GUIManager mgr(&meh);
GUIButton* bttn1 = mgr.addButton(22);
GUIButton* bttn2 = mgr.addButton(34);
while(true)
{
mgr.update();
if(_kbhit())
{
char c;
std::cin >> c;
if(c == 'a')
{
bttn1->onMouseUp();
}
else if(c == 's')
{
bttn2->onMouseUp();
}
else if(c == 'q')
{
break;
}
}
}
return 0;
}
|
Thank you for your time.
The GUIElement constructor doesn't initialize mgr.
Yes, thats it, love you man. Thank you very much.
I don't know how did i overlooked that.
Topic archived. No new replies allowed.