Jump to content
  • Advertisement

Archived

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

brassfish89

lets play find the access violation!

This topic is 5576 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

i''ve narrowed it down to 1 variable in the class. here''s the source:
gameover.h
----------------
class GameOver:public GameState
{

    // constructor

    GameOver()
    {
        init();
    }
    bool won,highscore;
    int score;
    DWORD waitWon,waitTime;
    char names[10][4];
    int scores[10];
    char temp2[4];
    int temp,place,currentLetter;
    char username[4]; // this is the variable

    // that causes the access violation when

    // i delete gameOver

public:

    // static GameOver pointer

    static GameOver* gameOver;
    
    // update GameOver screen

    void update();
    
    // render GameOver screen

    void render();
    
    
    // init GameOver screen

    void init()
    {
        waitTime=timeGetTime();
        currentLetter=0;
        for(int i=0;i<5;i++)
		{
            username[i]=''\0'';
			temp2[i]=''\0'';
		}
        CheckHighScore();
    }
    
    void SetWon(bool _won)
	{
		won=_won;
		if(won)
			waitWon=timeGetTime();
	}
    
    void SetScore(int _score){score=_score;}
    
    void CheckHighScore();
    
    void ProcessLetter(char c);
    
    // init GameOver object

    static void InitGameOver()
	{
		if(!gameOver)
			gameOver=new GameOver;
		int a=2;
	}

};
gameover.cpp
----------------

static char defNames[10][4]={"ABC","DEF","GHI","JKL","MNO","PQR","STU","VWX","YZA","BCD"};
static int defScores[10]={3000,2800,2600,2400,2200,2000,1800,1600,1400,1200};

GameOver* GameOver::gameOver=NULL;

void GameOver::update()
{

}

void GameOver::render()
{
    graphics.Clear(D3DCOLOR_XRGB(0,0,0));
    if(SUCCEEDED(graphics.BeginScene()))
    {
        RECT rect={0,0,ScreenW,ScreenH/2};
        graphics.WriteText(&mainF,&rect,"Please enter you initials:",-1,DT_VCENTER|DT_CENTER);
        rect.left=ScreenW/2-23;
        rect.top=ScreenH/2+18;
        rect.bottom=ScreenH/2+23;
        rect.left+=15*currentLetter;
        rect.right=rect.left+12;
        D3DRECT r={rect.left,rect.top,rect.right,rect.bottom};
        graphics.Clear(D3DCOLOR_XRGB(255,255,255),1,&r);
        rect.left=ScreenW/2-23;
        rect.top=ScreenH/2-5;
        rect.bottom=ScreenH/2+16;
        for(int i=0;i<4;i++)
        {
            rect.left+=15*i;
            rect.right=rect.left+15;
            if(username[i]!=''\0'')
            {
                char buffer[2];
                wsprintf(buffer,"%c",username);
                graphics.WriteText(&mainF,&rect,buffer,1,DT_BOTTOM|DT_CENTER,D3DCOLOR_XRGB(255,255,255));
            }
        }
        graphics.EndScene();
        graphics.Flip();
    }
}

void GameOver::CheckHighScore()
{
    ifstream in("hgsre.dat");
    if(!in)
    {
        ofstream out("hgsre.dat");
        out.write((char*)defNames,sizeof(defNames));
        out.write((char*)defScores,sizeof(defScores));
        out.close();
        for(int i=0;i<10;i++)
        {
            for(int j=0;j<5;j++)
            {
                names[i][j]=defNames[i][j];
            }
            scores[i]=defScores[i];
        }
    }
    else
    {
        in.read((char*)names,sizeof(names));
        in.read((char*)scores,sizeof(scores));
    }
    // check to see if the player''s score is greater than

    // the lowest one

    if(score<scores[9])
    {
        highscore=0;
        return;
    }
    temp=scores[0];
    // find the slot the player should go in

    for(int i=9;i>-1;i--)
    {
        if(score<scores[i])
        {
            place=i+1;
            temp=scores[place];
            scores[place]=score;
            for(int j=0;j<5;j++)
                temp2[j]=names[place][j];
            break;
        }
    }
    highscore=1;
}
void GameOver::ProcessLetter(char c)
{
    if(timeGetTime()-waitTime<=200)
        return;
    if(timeGetTime()-waitWon<=2000)
       return;
    if(!highscore)
        return;
    if(c!=''\n''&&c!=8&¤tLetter<3)
    {
        username[currentLetter]=c;
        if(currentLetter<2)
            currentLetter++;
    }
    else if(c==8&¤tLetter>0)
    {
        // backspace pressed (backspace==8)

        currentLetter--;
        username[currentLetter]=''\0'';
    }
}
thx brassfish
doh, nuts. Mmmm... donuts My website

Share this post


Link to post
Share on other sites
Advertisement


for(int i=0;i<5;i++)
{
username='\0';
temp2[i]='\0';
}



You are setting 5 elements of temp2 and username(i=0-4), when you have only allocated 4 (array 0-3)?


[edited by - Waverider on June 18, 2003 12:08:00 PM]

Share this post


Link to post
Share on other sites
ok, i fixed it
quote:

Let's play "use the debugger"!


i did use the debugger. ide been trying to figure out whats wrong for 2 days. it didn't show anything wrong with the array (although that was the first time i'd used the debugger for .NET 03).
EDIT-misspelled quote in the [ quote ] tags


doh, nuts. Mmmm... donuts
My website

[edited by - brassfish89 on June 18, 2003 4:24:25 PM]

Share this post


Link to post
Share on other sites
The best way to solve these problems is to step through the code line by line using the Debugger until you hit the line that causes the access violation. 99% of the time the fix is trivial after this. It''s a pretty straightforward method and almost always works. That''s what he meant by "use the debugger".

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!