Thread: Access Violation writing to a member variable

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    Access Violation writing to a member variable

    I am getting a access violation error when I run my program. I am using Visual C++ 2010. The compiler is giving me this error:

    First-chance exception at 0x00291709 in RobotWars.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.
    Unhandled exception at 0x00291709 in RobotWars.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.

    I'm new, so this looks like Greek. My compiler is pointing to this code:
    Code:
    void GameDrawer::setLevel(Level* level)
    {
    	m_currentLevel = level;
    }
    The .h file for GameDrawer:
    Code:
    #ifndef GAMEDRAWER_H
    #define GAMEDRAWER_H
    
    #include "Level.h"
    #include "Game.h"
    #include "glut.h"
    
    
    //The main drawer for the game, draws everything to the screen.
    class GameDrawer
    {
    
    	Level* m_currentLevel;			//the current level to draw
    
    	//drawing functions
    	void drawFloor();
    	void drawCeiling();
    
    public:
    
    	GameDrawer();
    	~GameDrawer();
    	
    	void setLevel(Level* level);			//sets the current level to draw
    	void draw();							//draws the current level/menu to the screen
    
    };
    
    #endif
    The function that the compiler is pointing to is invoked in the Game object's constructor (with related function definitions):

    Code:
    Game::Game()
    {
    	m_currentLevelNum = 0;
    	m_currentLevel = 0;
    	createLevel(64,64);
    }
    
    void Game::createLevel(short w, short h)
    {
    	if (m_currentLevel)
    		destroyLevel();
    
    	m_currentLevel = new Level(w,h);
    	m_drawer->setLevel(m_currentLevel);
    }
    
    void Game::destroyLevel()
    {
    	delete m_currentLevel;
    	m_currentLevel = 0;
    }
    I can edit this post with more information as needed. I really don't know what could be causing this. Any help would be appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > First-chance exception at 0x00291709 in RobotWars.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd.
    Win32 Debug CRT Heap Internals

    CD is the pattern memory is filled with when it is first allocated. It means you have an uninitialised pointer, and you tried to dereference it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Access violation writing location 0xcdcdcdcd.
    Access violation means that you're having trouble accessing memory. Take a look at the pointer. It looks like filler data, which tells me you're using uninitialized data somewhere.
    My guess is that you've made a pointer to a GameDrawer object somewhere but haven't initialized it yet - ie you're calling a member function for an object that doesn't exist.

    EDIT: Should probably refresh to see if someone else has replied when I let the edit window sit for 10 minutes.
    Consider this post signed

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Thanks for the clarification, I used to get this error alot, helps now that I know what it means.
    Ok, another question. Access violation 0X00000000 means I am trying to write to a null pointer correct? I ask because, now I am getting this
    error in the GameDrawer class, when I am trying to init a pointer. Here's the code.

    Code:
    GameDrawer::GameDrawer()
    {
    	setLevel(0);
    }
    
    void GameDrawer::setLevel(Level* level)
    {
    	m_currentLevel = level;
    }
    Last edited by insanoflex; 01-20-2011 at 11:11 AM. Reason: update question

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In that case you're most likely calling the method from a NULL pointer.
    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"

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Yeah, that's what it was, I forgot to init a pointer to an object. Thanks for the help guys.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Just wanted to say THANK YOU for that helpful hint on initializing pointers. After several hours of wrestling with this problem, I was able to solve it in less than two minutes with your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers
    By MrMatt027 in forum C++ Programming
    Replies: 14
    Last Post: 12-10-2010, 04:32 PM
  2. Memory access violation
    By mkb91 in forum C Programming
    Replies: 4
    Last Post: 10-13-2010, 11:23 AM
  3. cannot access private member declared in class
    By newme in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2008, 03:57 PM
  4. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM