Thread: Segmentation fault

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Segmentation fault

    When I debug my program it says this

    Code:
    Program received signal SIGSEGV, Segmentation fault.
    In ntdll!RtlEnumerateGenericTableWithoutSplaying () (C:\Windows\system32\ntdll.dll)
    I would probably know how to fix it, but it says the problem is in the system32 folder, and I don't want to do anything in there if I'm not sure what I'm doing.

    Could some one help me fix this?

    Thanks in advance

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Odds are, given that millions of people use ntdll without any trouble and you're the only person having trouble with your code, that the problem is actually in your code and not the system code. It's not impossible that your code is blameless, but I would still look there.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Alright, here's my code (sorry, it's a bit sloppy)

    HangMan.h
    Code:
    #ifndef HANGMAN_H
    #define HANGMAN_H
    
    #include <windows.h>
    #include <vector>
    #include "GameEngine.h"
    #include "Bitmap.h"
    #include "Resource.h"
    
    using namespace std;
    
    //Global Variables
    HINSTANCE    g_hInstance;
    Bitmap*      g_pMan[6];
    Bitmap*      g_pBlankSpace;
    int          g_iMissedLetters = 0;
    int          IDC_CONTROL;
    LPSTR        g_szPuzzle;
    BOOL         g_bGameOver = FALSE;
    BOOL         g_bGameStarted = FALSE;
    CHAR*        g_cInputLetter;
    char         g_cAlphabet[27];
    vector<char> g_vSingleLetter;
    GameEngine*  g_pGame;
    
    //Method Declerations
    BOOL ValidLetter(LPSTR szCheckString);
    void GameOver();
    void GameRestart();
    void GetLetters();
    
    #endif HANGMAN_H
    HangMan.cpp

    Code:
    #include "HangMan.h"
    
    void GameOver()
    {
        g_bGameOver = TRUE;
        g_bGameStarted = FALSE;
    }
    
    void GameRestart()
    {
        vector<char>::iterator viIterator;
        for(viIterator = g_vSingleLetter.begin(); viIterator != g_vSingleLetter.end(); viIterator++)
        {
            (*viIterator) = NULL;
            g_vSingleLetter.erase(viIterator);
            viIterator--;
        }
    
        g_iMissedLetters = 0;
    }
    
    void GetLetters()
    {
        char cSingleLetter;
    
        for(int i = 0; i < strlen (g_szPuzzle); i++)
        {
            cSingleLetter = g_szPuzzle[i];
            g_vSingleLetter.push_back(cSingleLetter);
        }
    }
    
    BOOL ValidLetter(LPSTR szCheckString)
    {
        for(int i = 0; i < 27; i++)
        {
            if(szCheckString[0] == g_cAlphabet[i])
              return TRUE;
        }
    
        MessageBox(g_pGame->GetWindow(), TEXT("You Entered an invalid charecter (Please use all caps)"), TEXT("Error"), MB_OK);
        g_bGameStarted = FALSE;
        return FALSE;
    }
    
    BOOL Initialize(HINSTANCE hInstance)
    {
        g_pGame = new GameEngine(hInstance, TEXT("Hang Man"), TEXT("Hang Man"), IDI_HANGMAN, IDI_HANGMAN_SM,
                                 640, 480);
    
        if(g_pGame == NULL)
          return FALSE;
    
        g_hInstance = hInstance;
    
        g_pGame->SetFrameRate(5);
    
        IDC_CONTROL = GetDlgCtrlID(g_pGame->GetWindow());
    
        srand(GetTickCount());
    
        return TRUE;
    }
    
    void GameStart(HWND hWindow)
    {
        HDC hDC = GetDC(hWindow);
        g_pMan[0] = new Bitmap(IDB_HEAD, 150, 75, TRUE, g_hInstance);
        g_pMan[1] = new Bitmap(IDB_BODY, 150, 240, TRUE, g_hInstance);
        g_pMan[2] = new Bitmap(IDB_ONELEG, 150, 300, TRUE, g_hInstance);
        g_pMan[3] = new Bitmap(IDB_LEGS, 150, 300, TRUE, g_hInstance);
        g_pMan[4] = new Bitmap(IDB_ONEARM, 150, 300, TRUE, g_hInstance);
        g_pMan[5] = new Bitmap(IDB_FULLBODY, 150, 300, TRUE, g_hInstance);
        g_pBlankSpace = new Bitmap(IDB_BLANKSPACE, 150, 300, FALSE, g_hInstance);
    
        g_cAlphabet[0] = ' ';
        g_cAlphabet[1] = 'A';
        g_cAlphabet[2] = 'B';
        g_cAlphabet[3] = 'C';
        g_cAlphabet[4] = 'D';
        g_cAlphabet[5] = 'E';
        g_cAlphabet[6] = 'F';
        g_cAlphabet[7] = 'G';
        g_cAlphabet[8] = 'H';
        g_cAlphabet[9] = 'I';
        g_cAlphabet[10] = 'J';
        g_cAlphabet[11] = 'K';
        g_cAlphabet[12] = 'L';
        g_cAlphabet[13] = 'M';
        g_cAlphabet[14] = 'N';
        g_cAlphabet[15] = 'O';
        g_cAlphabet[16] = 'P';
        g_cAlphabet[17] = 'Q';
        g_cAlphabet[18] = 'R';
        g_cAlphabet[19] = 'S';
        g_cAlphabet[20] = 'T';
        g_cAlphabet[21] = 'U';
        g_cAlphabet[22] = 'V';
        g_cAlphabet[23] = 'W';
        g_cAlphabet[24] = 'X';
        g_cAlphabet[25] = 'Y';
        g_cAlphabet[26] = 'Z';
    }
    
    void GameEnd()
    {
        vector<char>::iterator viIterator;
        for(viIterator = g_vSingleLetter.begin(); viIterator != g_vSingleLetter.end(); viIterator++)
        {
            g_vSingleLetter.erase(viIterator);
            viIterator--;
        }
        for(int i = 0; i < 6; i++)
            delete g_pMan[i];
    
        delete g_pBlankSpace;
        delete g_pGame;
    
        delete g_szPuzzle;
        delete g_cInputLetter;
    
        g_vSingleLetter.clear();
    }
    
    void GamePaint(HDC hDC)
    {
        //Draw the man being hanged
        if(g_iMissedLetters == 1)
          g_pMan[0]->Draw(hDC, 0, 0);
        else if(g_iMissedLetters == 2)
          g_pMan[1]->Draw(hDC, 0, 0);
        else if(g_iMissedLetters == 3)
          g_pMan[2]->Draw(hDC, 0, 0);
        else if(g_iMissedLetters == 4)
          g_pMan[3]->Draw(hDC, 0, 0);
        else if(g_iMissedLetters == 5)
          g_pMan[4]->Draw(hDC, 0, 0);
        else if(g_iMissedLetters == 6)
        {
            g_pMan[5]->Draw(hDC, 0, 0);
            GameOver();
        }
    
        //Draw The letters/ blak spaces for the hang man game
        if(g_bGameStarted)
        {
            for(int i = 1; i < strlen(g_szPuzzle); i++)
            {
                vector<char>::iterator viIterator;
                for(viIterator = g_vSingleLetter.begin(); viIterator != g_vSingleLetter.end(); viIterator++)
                {
                    if(*viIterator == NULL)
                      g_pBlankSpace->Draw(hDC, 300 + (g_pBlankSpace->GetWidth() * i), 470);
                    else
                      TextOut(hDC, 300 + (g_pBlankSpace->GetWidth() * i), 470, (LPSTR)(*viIterator), 1);
                }
            }
        }
    }
    
    void GameCycle()
    {
        if(!g_bGameStarted)
        {
    
            GetDlgItemText(g_pGame->GetWindow(), IDC_CONTROL, g_szPuzzle, 25);
            //Put the letters in the vector
            GetLetters();
            g_bGameStarted = TRUE;
        }
    
        else
        {
            GetDlgItemText(g_pGame->GetWindow(), IDC_CONTROL, g_cInputLetter, 1);
            //Check if the letters in the vector are letters
            ValidLetter(g_cInputLetter);
        }
    
        InvalidateRect(g_pGame->GetWindow(), NULL, FALSE);
    }
    
    void HandleKeys()
    {
    }
    
    void GameFocus(HWND hWindow)
    {
    }
    
    void GameNotFocus(HWND hWindow)
    {
    }
    
    void MouseDown(int x, int y, BOOL bLeft)
    {
    }
    
    void MouseUp(int x, int y, BOOL bLeft)
    {
    }
    I attached the other files to save space, I hope you don't mind

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by bijan311 View Post
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    In ntdll!RtlEnumerateGenericTableWithoutSplaying () (C:\Windows\system32\ntdll.dll)
    I would probably know how to fix it, but it says the problem is in the system32 folder, and I don't want to do anything in there if I'm not sure what I'm doing.
    Emphasis mine

    No there is not a problem with ntdll.dll, what you have above is a symptom of a bug in your program.

    What have you done to try and narrow down the cause of the problem? Have you started taking our pieces of code until the problem stops happening etc?

    This might appear to work, but it's technically wrong. You sholdn't be doing anything with an invalidated iterator:
    Code:
        vector<char>::iterator viIterator;
        for(viIterator = g_vSingleLetter.begin(); viIterator != g_vSingleLetter.end(); viIterator++)
        {
            g_vSingleLetter.erase(viIterator);
            viIterator--;
        }
    One way to fix it is:
    Code:
        vector<char>::iterator viIterator;
        for (viIterator = g_vSingleLetter.begin(); viIterator != g_vSingleLetter.end();)
        {
            g_vSingleLetter.erase(viIterator++);
        }
    However, this can be replaced entirely by just:
    Code:
        g_vSingleLetter.clear();
    which you already have further down, so you can actually just delete those lines!
    Last edited by iMalc; 12-12-2010 at 11:51 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM