Skip to main content
GameDev.net gamedev.net
🔒 Locked

Access violation.... again....

Started by Grant1219 Aug 14, 2006 at 9:40 PM 5 replies 700+ views
Original Post
Grant1219
Grant1219
I am making a basic framework that I can just use for making any type of game using the Allegro graphics library. Right now it has a message handler class, application class, and event handler class. I got the idea from an SDL book I read, but changed it quite a bit. Here is all the code that relates to the problem. ~~MessageHandler.h~~ //include guards #ifndef MESSAGEHANDLER_H #define MESSAGEHANDLER_H #include "allegro.h" //the message ID variable typedef unsigned int MSGID; //the message parameter variable typedef void *MSGPARM; /* CMessageHandler makes it easy for classes to communicate with each other. It handles the setting of message IDs, the parameters that go with the messages, and the sending and handling of messages. */ class CMessageHandler { private: static MSGID m_nextMSGID; public: //handles messages, this is meant to be overidden virtual bool OnMessage(MSGID msgID, MSGPARM parm1, MSGPARM parm2, MSGPARM parm3, MSGPARM parm4, MSGPARM parm5); //sends messages bool SendMessage(MSGID msgID, MSGPARM parm1, MSGPARM parm2, MSGPARM parm3, MSGPARM parm4, MSGPARM parm5); //gets a new MSGID static MSGID GetNextMSGID(); }; #endif ~~MessageHandler.cpp~~ #include "messagehandler.h" //sets the message ID to 0 MSGID CMessageHandler::m_nextMSGID = 0; //this returns a new message ID for a message MSGID CMessageHandler::GetNextMSGID() { //return a new MSGID return(m_nextMSGID++); } //sends messages (I think this function has something to do with the access violation) bool CMessageHandler::SendMessage(MSGID msgID, MSGPARM parm1, MSGPARM parm2, MSGPARM parm3, MSGPARM parm4, MSGPARM parm5) { //process the message if(OnMessage(msgID,parm1,parm2,parm3,parm4, parm5)) { return(true); } //if the message is unknown else { return(false); } } //handles messages, meant to be overidden bool CMessageHandler::OnMessage(MSGID msgID, MSGPARM parm1, MSGPARM parm2, MSGPARM parm3, MSGPARM parm4, MSGPARM parm5) { //returns true by default return(true); } ~~EventHandler.h~~ //include guards #ifndef EVENTHANDLER_H #define EVENTHANDLER_H #include "application.h" #include "messagehandler.h" /* CEventHandler handles all keyboard, mouse, and custom events */ class CEventHandler : public CMessageHandler { public: //check for events void CheckEvent(); //keyboard events void OnKeyDown(); void OnKeyUp(); //mouse events void OnMouseMove(); void OnLButtonDown(); void OnRButtonDown(); void OnLButtonUp(); void OnRButtonUp(); //handles messages bool OnMessage(MSGID msgID, MSGPARM parm1, MSGPARM parm2, MSGPARM parm3, MSGPARM parm4, MSGPARM parm5); //messages static MSGID MSGID_EXIT; }; #endif ~~EventHandler.cpp~~ #include "eventhandler.h" //get message IDs MSGID CEventHandler::MSGID_EXIT = CMessageHandler::GetNextMSGID(); void CEventHandler::CheckEvent() { if(keypressed()) OnKeyDown(); if(mouse_b&1) OnLButtonDown(); if(mouse_b&3) OnRButtonDown(); } void CEventHandler::OnKeyDown() { int keyVal; keyVal = readkey(); if ((keyVal >> 8) == KEY_ESC) { //SendMessage is what is causing the access violation, it really shouldn't have to do with MSGID_EXIT being NULL because that wouldn't effect it. It must be the SendMessage function, from what I see. SendMessage(MSGID_EXIT, NULL, NULL, NULL, NULL, NULL); } } void CEventHandler::OnMouseMove() { } void CEventHandler::OnLButtonDown() { } void CEventHandler::OnRButtonDown() { } bool CEventHandler::OnMessage(MSGID msgID, MSGPARM parm1, MSGPARM parm2, MSGPARM parm3, MSGPARM parm4, MSGPARM parm5) { if(msgID == MSGID_EXIT) { CApplication::GetApp()->Exit(); return(true); } else { return(false); } } It seems I cant make anything without having these access violation problems and can never seem to fix them myself, I really don't like always having to always post in the forums for help, but I don't know what else to do. It's kind of discouraging not being able to fix your own problems, for me anyway. Any help and an explaination of why the access violation was caused would be great. Thanks in advance. P.S. How do you put the code in those neat litte boxes so the formatting and colors stay nice and readable?
Grant1219
Grant1219
If I take out SendMessage(MSGID_EXIT, NULL, NULL, NULL, NULL, NULL); in EventHandler.cpp and replace it with CApplication::GetApp()->Exit(); it works fine, but I need to get the message handler working.
Grant1219
Grant1219
Maybe the problem is that I don't really know how to use the VC++ debugger very well. I did use it to find out that SendMessage() is the problem, but then it gives me the disassembly for the SendMessage() function and I'm not any good with reading assembly. Is there anything else I could try to pinpoint which variable is causing the problem?
Grant1219
Grant1219
Ok, I have finally figured out why these access violations are happening, they pretty much happen whenever I try to do any OOP programming at all, so it has to be a problem with VC++, a setting must be not right or something.

I'll explain what I found out. I'll use the CEventHandler class just because its already posted. If I declare any variable within the CEvent handler class like say an integer called "num", then try to access it within the CEventHandler functions it gives the access violation.

I'll show what I did to test this.

First I just put "int num;" into the CEventHandler class, as a public member too.

Then in the EventHandler.cpp file I made it so it sets "num = 5" when I press escape, like in the code below.

void CEventHandler::OnKeyDown(){	int keyVal;	keyVal = readkey();	if ((keyVal >> 8) == KEY_ESC)	{		num = 5;		CApplication::GetApp()->Exit();	}}


Then in the disassembly it showed where the access violation was happening.

Here is the assembly, I marked where it marked.

CEventHandler::CheckEvent:
004175F0 push esi
004175F1 mov esi,ecx
004175F3 call dword ptr [__imp__keypressed (4263C8h)]
004175F9 test eax,eax
004175FB je CEventHandler::CheckEvent+54h (417644h)
004175FD call dword ptr [__imp__readkey (4263D0h)]
00417603 and eax,0FFFFFF00h
00417608 cmp eax,3B00h
0041760D jne CEventHandler::CheckEvent+43h (417633h)
-->0041760F mov dword ptr [esi+8],5
00417616 call CApplication::GetApp (40102Dh)
0041761B mov ecx,eax
0041761D call CApplication::Exit (40100Fh)

Obviously it tried to set the num variable to 5 and couldn't for some reason. It makes no sense because a class should be able to access its own members, somethings got to be going wrong within VC++. Any help or suggestions would really help, thanks.

MaulingMonkey
MaulingMonkey
Quote:
Original post by Grant1219
Obviously it tried to set the num variable to 5 and couldn't for some reason. It makes no sense because a class should be able to access its own members, somethings got to be going wrong within VC++.


Wrong. In this case, the most likely definate answer is that the "this" pointer is invalid when calling the function. The "this" pointer is implicitly used behind the scenes to access member variables - conceptually, num is just shorthand for this->num.

When the function is called ala:
pointer->OnKeyPress( ... )

pointer is the implicit "this" argument to the member function. Since an invalid "this" pointer is the problem in this case, it follows that you should find out why pointer is invalid.

Common causes
1) Never initialized
2) Underlying object has been delete ed (or gone out of scope)
3) The pointer was overwritten due to a buffer overflow or other super-naughty behavior (tm)

Also, please use [ source ] tags for posting large bodies of text. This preserves spacing, does syntax highlighting, and generaly makes it easier to eyeball your code.

Also, read Introduction to Debugging, a recent article by Superpig on the subject.
Grant1219
Grant1219
I fixed the access violation, I was creating an instance of CMessageHandler like this
CMessageHandler *msg;
instead of what I should have done, which is
CMessageHandler *msg = new CMessageHandler;
. Thanks for all your help.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.