Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

tonymontana

Again ACCESS VIOLATION problem..Can anyone explain what is wrong??

This topic is 5243 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is the code i wrote but it gives an Acess Violation error. when i tried to take the value of an integer(UpXco) member from the a class(PefectStones)..Can anyone tell me what is wrong?? i have indicated the line that causes error with ///////////////////////
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include <mmsystem.h>
#include "resource.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


/////////////////////////////////////CONSTANTS////////////////////////////////////////////////

const char* const WINDOWCAPTION="One of My Windows";
const char* const CLASSNAME="MY CLASS";

enum GAMESTATE {GAME_INITIALIZE,GAME_START,GAME_RUN,GAME_STOP,GAME_RESTART,GAME_EXIT};

//////////////////////////////////////GLOBALS/////////////////////////////////////////////////

int WindowCounter=0;  // Globally tracks Windows

HINSTANCE SparehInstance=NULL; // hInstance of Application

GAMESTATE StateOfGame=GAME_INITIALIZE; //State of the Game

RECT Ball;     //ball is an ellipse bounded by a square

RECT Concrete;//Concrete is the rectangle that is used by player

int Life;   //Life of Player

int Score;  //Score of Player

int VelocityX;  // Velocity  in X direction for ball

int VelocityY;  // Velociy   in Y direction for ball

////////////////////////////////////DECLERATIONS///////////////////////////////////////////

int GameMain(HWND&);

////////////////////////////////GAME RELEATED STUFF/////////////////////////////////////////

class CSpecialStone  //Special Stones That has Special Effects

{
private:
	int UpXco,UpYco,DownXco,DownYco; //Coordinates of A rectangle up==>upleftco  down=downright

	char SpecialFeature;
public:
	int VelocityX; //XVelocity of the Stone;

	int VelocityY; //YVelocity of the Stone;

	CSpecialStone* Next;          //Next Special Stone (will be used in a linklist)

	CSpecialStone():UpXco(0),UpYco(0),DownXco(0),DownYco(0),Next(NULL)
	{
		VelocityY=rand()%20+5;
	    VelocityX=rand()%20+5;
		int TempInt=rand()%3;
		switch(TempInt)   //The Special Feature of Stone is arranged by that

		{
		case 0:
			{
				SpecialFeature=''S''; //That will increase Score  100

			}
			break;
		case 1:
			{
				SpecialFeature=''V''; //That will Decrease the Velociy of the ball;

			}
			break;
		case 2:
			{
				SpecialFeature=''L'';// That will Ýncrease The life of player;

				break;
			}
		}
	}
	CSpecialStone(int UpX,int Upy,int DownX,int DownY):UpXco(UpX),	UpYco(Upy),DownXco(DownX),
		DownYco(DownY)
	{
		int TempInt=rand()%3;
		switch(TempInt)
		{
		case 0:
			{
				SpecialFeature=''S''; //That will increase Score  100

			}
			break;
		case 1:
			{
				SpecialFeature=''V''; //That will Decrease the Velociy of the ball;

			}
			break;
		case 2:
			{
				SpecialFeature=''L'';// That will Ýncrease The life of player;

				break;
			}
		}
	}
	CSpecialStone(int UpX,int Upy,int DownX,int DownY,CSpecialStone* NextStone):UpXco(UpX),
		UpYco(Upy),DownXco(DownX),DownYco(DownY),Next(NextStone)
	{
		int TempInt=rand()%3;
		switch(TempInt)
		{
		case 0:
			{
				SpecialFeature=''S''; //That will increase Score  100

			}
			break;
		case 1:
			{
				SpecialFeature=''V''; //That will Decrease the Velociy of the ball;

			}
			break;
		case 2:
			{
				SpecialFeature=''L'';// That will Ýncrease The life of player;

				break;
			}
		}
	}
	void MovebyAdding(int,int,int,int);//Move the rectangle by adding increasin/decreasin Coordinates

	int RetUpX();
	int RetUpY();
	int RetDownX();
	int RetDownY();
	char RetSpecialFeature();
};
int CSpecialStone::RetUpX()/////////////////////////////////////////THAT LINE CAUSES ACCESS VIOLATION WHY???

{
	return UpXco;
}
int CSpecialStone::RetUpY()
{
	return UpYco;
}
int CSpecialStone::RetDownX()
{
	return DownXco;
}
int CSpecialStone::RetDownY()
{
	return DownYco;
}
char CSpecialStone::RetSpecialFeature()
{
	return SpecialFeature;
}
void CSpecialStone::MovebyAdding(int UpX,int UpY,int DownX,int DownY)
{
	UpXco+=UpX;
	UpYco+=UpY;
	DownXco+=DownX;
	DownYco+=DownY;
}

class CSpecialStones      // That is a LinkList of Special Stones

{
private:
	CSpecialStone* First;
	static int StoneCounter; //Counts the number of Special Stones

public:
	CSpecialStones()
	{
		++StoneCounter;
		First=new CSpecialStone;//We are Creating the first element to Eradicate the problem

		                        //of Access Vilolation..These first will be next of the

	}	                        //member that we created with Create() function if set this

	                            //to NULL ACCESS VIOLATION OCCURS...

	CSpecialStone* operator [](int index);
	CSpecialStone& Create();  //Creates a CSpecialStone by Default Rectangle Size

	CSpecialStone& Delete();  //Deletes the First CSpecialStone(the stone on top)

	CSpecialStone& Create(int UpX,int UpY,int DownX,int DownY);//Overloaded Version..

	int RetStoneCounter();
};
int CSpecialStones::StoneCounter=0;
int CSpecialStones::RetStoneCounter()
{
	return StoneCounter;
}
CSpecialStone* CSpecialStones::operator[](int index)
{
	int TempCounter=StoneCounter;
	int NumberOfTimesToGoBack=StoneCounter-index;
/* To reach The index member of linklist we must go (StoneCounter-index) times back...
   EX: index=2 and StoneCounter=5 to reach 2.member we must go 3(StoneCounter-index) times back
*/  CSpecialStone* SearchedStone=First;   //Note: NOT LIKE THE ARRAYS... THERE WILL BE NO

	do                                    //      CSPecialStones[0] element but if it has 5 

	{                                     //      member THERE WILL BE CSpecialStones[5]...

		SearchedStone=SearchedStone->Next;
		--TempCounter;
	}while(TempCounter==NumberOfTimesToGoBack);
	return (SearchedStone);
}
CSpecialStone& CSpecialStones::Create()  //Returns the Created stone as a reference

{
	++StoneCounter;
	First->Next=First;
	First=new CSpecialStone(0,0,20,20);
	return *(First);
}
CSpecialStone& CSpecialStones::Delete()  //Returns the Next that became First after destruction

{                                        //of  Old First...

	--StoneCounter;
	CSpecialStone* TempStone=First;  // a tempstone takes the original First to delete

	First=First->Next; //First takes the place of Next Because original First will be deleted

	delete (TempStone);
	return (*First);
}
CSpecialStone& CSpecialStones::Create(int UpX,int UpY,int DownX,int DownY)
{
	++StoneCounter;
	First->Next=First;
	First=new CSpecialStone(UpX,UpY,DownX,DownY);
	return (*First);
} 

////////////////////////////////////WINPROC//////////////////////////////////////////////////


LRESULT CALLBACK WinProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)
{
	static GAMESTATE TempGameState=StateOfGame;
	PAINTSTRUCT ps;
	switch(msg)
	{
	case WM_CREATE:
		{
			++WindowCounter;
			return 0;
		}
		break;
	case WM_PAINT:
		{
			HDC hdc;
			hdc=BeginPaint(hwnd,&ps);
			RedrawWindow(hwnd,&ps.rcPaint,NULL,RDW_INVALIDATE);
			EndPaint(hwnd,&ps);
			ReleaseDC(hwnd,hdc);
			return 0;
		}
		break;
	case WM_ACTIVATE:
		{
			HDC hdc=GetDC(hwnd);
			char buffer[60];
			SetTextColor(hdc,RGB(220,220,220));
			switch(wparam)
			{
			case WA_ACTIVE:
				{
					sprintf(buffer,"Window is Activated");
					TextOut(hdc,50,50,buffer,strlen(buffer));
					return 0;
				}
				break;
			case WA_CLICKACTIVE:
				{
					sprintf(buffer,"Window is Activated by Mouse");
					TextOut(hdc,50,50,buffer,strlen(buffer));
					return 0;
				}
				break;
			case WA_INACTIVE:
				{
					sprintf(buffer,"Window is Deactivated");
					TextOut(hdc,50,50,buffer,strlen(buffer));
					return 0;
				}
				break;
			}
			return 0;
		}
		break;
	case WM_KEYDOWN:
		{
			int virtual_code=int(wparam);
			int key_state=int(lparam);
			//Collision Detection if user wants to move the Concrete While Ball and Concrete in

			//contact Ball will move up...

			if( ((Ball.right>Concrete.left)&&(Ball.bottom>Concrete.top)) && 
				(Ball.left<Concrete.right)  )
			{
				Ball.top-=30;
				Ball.bottom-=30;
			}
			switch(virtual_code)
			{
			case VK_RIGHT:
				{
					Concrete.left+=30;
					Concrete.right+=30;
					return 0;
				}
				break;
			case VK_LEFT:
				{
					Concrete.left-=30;
					Concrete.right-=30;
					return 0;
				}
				break;
			case VK_UP:
				{
					Concrete.top-=30;
					Concrete.bottom-=30;
					return 0;
				}
				break;
			case VK_DOWN:
				{
					Concrete.top+=30;
					Concrete.bottom+=30;
					return 0;
				}
				break;
			}
			return 0;
		}
		break;
	case WM_CLOSE:
		{
			int Answer=0;
			Answer=MessageBox(hwnd,"Are You Sure??","Sure??",MB_YESNO);
			if(Answer==IDYES)
			{
				SendMessage(hwnd,WM_DESTROY,wparam,lparam);
				
			}
			return 0;
		}
		break;
	case WM_DESTROY:
		{

			--WindowCounter;
			if(WindowCounter<=0)
			{
				PostMessage(hwnd,WM_QUIT,wparam,lparam);
			}
			return 0;
		}
		break;
	}
	return (DefWindowProc(hwnd,msg,wparam,lparam));
}


/////////////////////////////////WINPROC////////////////////////////////////////////////////


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	srand(time(NULL));
	SparehInstance=hInstance;
	WNDCLASSEX myclass;
	MSG msg;
	HWND hwnd;
	myclass.cbClsExtra=0;
	myclass.cbSize=sizeof(WNDCLASSEX);
	myclass.cbWndExtra=0;
	myclass.hbrBackground=(HBRUSH) GetStockObject(WHITE_BRUSH);
	myclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	myclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	myclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
	myclass.hInstance=hInstance;
	myclass.lpfnWndProc=WinProc;
	myclass.lpszClassName=CLASSNAME;
	myclass.lpszMenuName=NULL;
	myclass.style=CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
	if(!(RegisterClassEx(&myclass)))
	{
		MessageBox(NULL,"Registration Error","Reg.Err",MB_OK);
		return 0;
	}
	if(!(hwnd=CreateWindowEx(NULL,CLASSNAME,WINDOWCAPTION,WS_OVERLAPPEDWINDOW|WS_VSCROLL,
		CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL)))
	{
		MessageBox(NULL,"Creation Error","Crt.Err.",MB_OK);
		return 0;
	}
	ShowWindow(hwnd,SW_MAXIMIZE);
	UpdateWindow(hwnd);
	while(true)
	{
		if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
		{
			if(msg.message==WM_QUIT)
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
			GameMain(hwnd);
		}
	}
	return (msg.wParam);
}

////////////////////////////////DEFINATIONS//////////////////////////////////////////////////

int GameMain(HWND& myhwnd)
{
	HDC hdc=GetDC(myhwnd);
	static char buffer[80];   //will be used for Texts....(Score and Life)

	static RECT PlayArea;  // The Area Where The Ball Will be inside

	static HBRUSH ObBrush=CreateSolidBrush(RGB(100,100,100));
	static HPEN  ObPen=CreatePen(PS_SOLID,4,RGB(180,180,180));
	SelectObject(hdc,ObBrush);
	SelectObject(hdc,ObPen);
	CSpecialStones PerfectStones; 
	while(StateOfGame!=GAME_EXIT)
	{
		switch(StateOfGame)
		{
		case GAME_INITIALIZE:
			{
				GetClientRect(myhwnd,&PlayArea);
				Ball.left=50;         //it is 50*50 square

				Ball.top=50;
				Ball.right=100;
				Ball.bottom=100;
				Concrete.left=500;  // x2-x1=150 ;// The length of Concrete

				Concrete.top=500;   // y2-y1=25;  // The width  of Concrete

				Concrete.right=650;
				Concrete.bottom=525;
				VelocityX=(rand()%10)+10;
				VelocityY=(rand()%10)+10;
				Life=5;
				Score=0;
				StateOfGame=GAME_START;
				return 0;   //We are Returning from Function to continue Event Looop of Windows

			}
			break;
		case GAME_START:
			{
				StateOfGame=GAME_RUN;
				return 0;   //We are Returning from Function to continue Event Looop of Windows

			}
			break;
		case GAME_RUN:
			{
			   int LoopStartTime=GetTickCount();
			   int ProbabilityOfStoneCreation=rand()%25; // Süper taþ yaratýlacak mý??

			   if(ProbabilityOfStoneCreation<7)
			   {
				   PerfectStones.Create(rand()%20,rand()%50,(rand()%20)+20,(rand()%20)+50);
			   }
			   // Collision Detection between Stones and Play area

			    for(int cntr1=PerfectStones.RetStoneCounter();cntr1>0;--cntr1)
				{
					if(PerfectStones[cntr1]->RetUpX()<=PlayArea.left)
					{
						PerfectStones[cntr1]->VelocityX=-VelocityX;
					}
					if(PerfectStones[cntr1]->RetUpY()<=PlayArea.top)
					{
						PerfectStones[cntr1]->VelocityY=-VelocityY;
					}
					if(PerfectStones[cntr1]->RetDownX()>=PlayArea.right)
					{
						PerfectStones[cntr1]->VelocityX=-VelocityX;
					}
					if(PerfectStones[cntr1]->RetDownY()>=PlayArea.bottom)
					{
						PerfectStones.Delete();  // if it is so Deletes it.....

					} 
				}
				//Collision Detection between Stones and Concrete

				for(int cntr2=PerfectStones.RetStoneCounter();cntr2>0;--cntr2);
				{
					//That if Statement means if they are touched in anyway...

					if( ((PerfectStones[cntr2]->RetDownX()>Concrete.left)
						&&(PerfectStones[cntr2]->RetDownY()>Concrete.top)) && 
					((PerfectStones[cntr2]->RetUpX()<Concrete.right)) &&
					(PerfectStones[cntr2]->RetDownY()<=Concrete.bottom)  )
					{
						switch(PerfectStones[cntr2]->RetSpecialFeature())
						{
						case ''V'':     //Slowers the Ball

							{
								VelocityY-=5;
								VelocityY-=5;
							}
							break;
						case ''L'':
							{
								++Life;
							}
							break;
						case ''S'':
							{
								Score+=100;
							}
							break;
						}
					}
				}
				//Make Movement of Stones...

				for(int cntr3=PerfectStones.RetStoneCounter();cntr3>0;--cntr3)
				{
					PerfectStones[cntr3]->MovebyAdding(PerfectStones[cntr3]->VelocityX,
						PerfectStones[cntr3]->VelocityY,PerfectStones[cntr3]->VelocityX,
						PerfectStones[cntr3]->VelocityY);
				}
				//Rectangle to Redraw the Window (THAT MUST BE THE FIRST THING DRAWN)

				Rectangle(hdc,PlayArea.left,PlayArea.top,PlayArea.right,PlayArea.bottom);
				if(Ball.bottom>=PlayArea.bottom)  //To ensure the ball  will be in PlayArea

				{
					VelocityY=-VelocityY;
				}
				if(Ball.right>=PlayArea.right)
				{
					VelocityX=-VelocityX;
				}
				if(Ball.left<=PlayArea.left)
				{
					VelocityX=-VelocityX;
				}
				if(Ball.top<=PlayArea.top)
				{
					VelocityY=-VelocityY;
				}
				if(Concrete.left<=PlayArea.left)  //To ensure the Concrete will be in PlayArea

				{
					Concrete.left=0;
					Concrete.right=150;
				}
				if(Concrete.right>=PlayArea.right)
				{
					Concrete.right=1024;
					Concrete.left=874;
				}
				if(Concrete.top<=PlayArea.top)
				{
					Concrete.top=0;
					Concrete.bottom=25;
				}
				if(Concrete.bottom>=PlayArea.bottom)
				{
					Concrete.bottom=PlayArea.bottom;
					Concrete.top=PlayArea.bottom-25;
				}
//Collision Detection for Concrete and Ball...

//That will ensure if the ball and Concrete touched Each other

				if( ((Ball.right>Concrete.left)&&(Ball.bottom>Concrete.top)) && 
					((Ball.left<Concrete.right)) &&(Ball.top<=Concrete.bottom)  )
					//With if Statemen we ensure that ball striked the Concrete from upside

					//or downside

				{
					VelocityY=-VelocityY;//When Ball hit the Concrete Only Y coordinate Velocity

					                     //Will be inverted..(Because it bounces back in Yco..)

				}
//Collision Detection for SpecialStones and Concrete...

             
//Score and Life arregment

				if(Ball.top<=PlayArea.top)
				{
					Score+=20;
				}
				if(Ball.bottom>=PlayArea.bottom)
				{
					--Life;
				}
				Ball.left+=VelocityX;
				Ball.top+=VelocityY;
				Ball.right+=VelocityX;
				Ball.bottom+=VelocityY;
				sprintf(buffer,"Life: %d Score:%d",Life,Score);
				TextOut(hdc,20,20,buffer,strlen(buffer));
				//For Drawing Perfect Stones

				for(int cntr4=PerfectStones.RetStoneCounter();cntr4>0;--cntr4)
				{
					Rectangle(hdc,PerfectStones[cntr4]->RetUpX(),PerfectStones[cntr4]->RetUpY(),
						PerfectStones[cntr4]->RetDownX(),PerfectStones[cntr4]->RetDownY());
				}
				Ellipse(hdc,Ball.left,Ball.top,Ball.right,Ball.bottom);//For ball

				Rectangle(hdc,Concrete.left,Concrete.top,Concrete.right,Concrete.bottom);//For

				if(Life<=0)                                                     //Concrete

				{
					sprintf(buffer,"You Losed...");
					TextOut(hdc,20,20,buffer,strlen(buffer));
					StateOfGame=GAME_EXIT;
				}
				ReleaseDC(myhwnd,hdc);
				while(GetTickCount()-LoopStartTime<16);
				return 0;   //We are Returning from Function to continue Event Looop of Windows

			}
			break;
		case GAME_STOP:
			{
				return 0;
			}
			break;
		case GAME_EXIT:
			{
				return 0;
			}
			break;
		default:
			{
				return 0;
			}
			break;
		}
	}
	return 0;
}



				


the archer who shoots his arrow beyond his target is no more successful than the one who''s arrow didn''t even reach the target.

Share this post


Link to post
Share on other sites
Advertisement
CSpecialStones::operator[] is broken somehow as it returns NULL when you call it from GameMain() (at least the first time). Now when RetUpX() is called the this pointer is NULL and thus it cannot locate UpXco. (That''s why the error pops up in a weird place)

You should also move your call to GameMain() outside the if(PeekMessage()) -block:

//Change

if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
{
//...

GameMain(hwnd);
}

// to

if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE))
{
//...

}
GameMain(hwnd);

Otherwise GameMain() will be called only when messages arrive.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!