1

Please have a look at the following code.

GameComponent.h

#pragma once
#include<time.h>
#include "Position.h"

class GameComponent
{
public:
    GameComponent(int);
    GameComponent();
    ~GameComponent(void);

    virtual void update(const tm*);

    void addPosition(Position *p);

    friend class Position;

private:
    int id;
    Position *position;
};

GameComponent.cpp

#include "GameComponent.h"
#include <iostream>
#include <time.h>
#include "DrawableGameComponent.h"
#include "Position.h"

using namespace std;

GameComponent::GameComponent(int v):id(v)
{

}

GameComponent::GameComponent(){}


GameComponent::~GameComponent(void)
{

}

void GameComponent::update(const tm* time)
{
    cout << "ID : " << id << endl;
    cout << "Update: " << time->tm_hour << ":" << time->tm_min << ":" << time->tm_sec << endl;
    position->display();

    //Position::displayPositions();
}

void GameComponent::addPosition(Position *p)
{
    position = p;
    p->display();
    cout << "position working" << endl;
}

Position.h

#pragma once
class Position
{
    friend class GameComponent;
public:
    Position(int x, int y, int z);
    Position();
    ~Position();
    void display();

private:
    int x;
    int y;
    int z;
};

Position.cpp

#include "Position.h"
#include <iostream>

using namespace std;

Position::Position(int x, int y, int z)
{
    this->x = x;
    this->y = y;
    this->z = z;
}

Position::Position(){}


Position::~Position(void)
{
}

void Position::display()
{
    //cout << "Display Working" << endl;
    cout <<"Position " << " X: " << x << " Y: " << y << " Z: " << z << endl; 
}

First, this is not the complete code, this is a part of the code where error occurred. When I run my code I get the error

Unhandled exception at 0x00302d90 in GameEngine.exe: 0xC0000005: Access violation reading location 0xcdcdcdd5.

It is pointing to the output area (cout << "X" << ........) of the display() method of position (Visual Studio shows a yellow array pointing to that place).

I googled this issue, and found this happens because of null pointers, where it points to no where. There is a pointer assigning (assigning pointer parameter to another pointer) inside GameComponent's addPosition() method.

I believe the error comes from there. Please help me to get rid of this error

Update

Following is the location where I cann addPosition()

Test.cpp

GameComponent **component;

void Test::add(GameComponent *gameComponent, Position *p)
{

        component[componentCount] = gameComponent;
        component[componentCount]->addPosition(p);
        componentCount++;


}
PeakGen
  • 21,894
  • 86
  • 261
  • 463
  • 1
    http://stackoverflow.com/q/370195/10396 may help explain where the `oxCDCDCD` came from – AShelly Dec 21 '12 at 17:03
  • 1
    Can you show us where you call `addPosition`? – Joseph Mansfield Dec 21 '12 at 17:03
  • You have a lot of pointers and no `NULL` checks. Use a reference or perform a `NULL` check before using pointers. – andre Dec 21 '12 at 17:07
  • @sftrabbit: It is added. Please have a look :) – PeakGen Dec 21 '12 at 17:11
  • @ahenderson: Exception handling? Sorry, I don't know that yet. I am new to C++ – PeakGen Dec 21 '12 at 17:12
  • 2
    @Sepala I'm afraid now I'm going to ask to see your call to `add`. Basically, I want to know where the `Position*` has come from and if it's valid. – Joseph Mansfield Dec 21 '12 at 17:17
  • yES it is valid. Because inside the addPosition(Position *p) I can call methods in Position class, using the parameter "p". There is no error there. I just can't call any using "position" variable of GameComponent – PeakGen Dec 21 '12 at 17:22
  • @ahenderson, NULL checks won't help when the pointer is never initialized. – Mark Ransom Dec 21 '12 at 17:24
  • change `void GameComponent::addPosition(Position *p)` to `void GameComponent::addPosition(Position& p)` and `position = &p;` – andre Dec 21 '12 at 17:33
  • @ahenderson: did, error is still there – PeakGen Dec 21 '12 at 19:03
  • @sftrabbit: Hi, please comment out the working line of display() method, and uncomment the other line. It is working, that line get displayed! what's going on here?? – PeakGen Dec 21 '12 at 19:06
  • @Sepala It seems like you have undefined behaviour somewhere, which can make things behave weirdly (like this line that shouldn't make your program crash). This can be caused by many things, but most often it is caused by improper pointer usage. It could be *anywhere* in your program. Just because this line makes it crash, doesn't mean this is where the problem is. Everywhere that you use a pointer, check thoroughly that the pointer is valid. – Joseph Mansfield Dec 21 '12 at 19:12
  • @sftrabbit: ohh my goodness :( – PeakGen Dec 21 '12 at 19:22

1 Answers1

5

0xCDCDCDCD is a special value placed around allocated memory by MSVC to alert you (and the debug runtime) when invalid memory is being accessed.

Since your address is 0x08 higher, I suspect you're adding to a pointer or array and attempting to dereference it. This may be an off-by-one error, attempting to grab an item past the end of an array, or it could be an object being used after deallocation.

I googled this issue, and found this happens because of null pointers, where it points to no where. There is a pointer assigning (assigning pointer parameter to another pointer) inside GameComponent's addPosition() method.

Check to make sure this pointer is valid and isn't 0xCDCDCDCDor pointing to memory filled with that (or other guard) values. If the compiler is padding your Position or inserting a vtable reference, the address given in the segfault matches where X would be when dereferencing 0xCDCDCDCD.

ssube
  • 47,010
  • 7
  • 103
  • 140
  • Hi, please comment out the working line of display() method, and uncomment the other line. It is working, that line get displayed! what's going on here?? – PeakGen Dec 21 '12 at 19:06