Jump to content
  • Advertisement
Sign in to follow this  
Iccarus

Problem writing to location

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

"Unhandled exception at 0x0041e019 in map.exe: 0xC0000005: Access violation writing location 0xcdcdcdd9." I'm getting the above error in my program when I try and set values of variables in a class through a second class. The code is:
void MAP::AddRooms()
{
	int length = (maxy * maxx) + 2;
	for (int i = 1;i < length;i++)
	{
		int type = GetMapArrayElement(i);		
		roomlist[i-1]->SetY(i);
		roomlist[i-1]->SetType(type);
		roomlist[i-1]->SetExplored(false);
		roomlist[i-1]->SetPlayerin(false);
		roomlist[i-1]->SetMap(GetMap());
		roomlist[i-1]->SetX(i);			
	}
}
for calling the functions to set the variables from within the second class
void ROOM::SetX(int xnumber)
{
	x = xnumber;
}
void ROOM::SetY(int ynumber)
{
	y = ynumber;
}
void ROOM::SetMap(int mnumber)
{
	map = mnumber;
}
void ROOM::SetExplored(bool value)
{
	explored = value;
}
void ROOM::SetType(int typeno)
{
	type = typeno;
}
void ROOM::SetPlayerin(bool value)
{
	playerin = value;
}
For setting the variables within their class. Debugging it, it crashes on trying to set any of those variables through the second class but if I try and do it normally it works fine.

Share this post


Link to post
Share on other sites
Advertisement
Quote:

0xcdcdcdd9


looks suspicious.

i'd say you have an initialised pointer somewhere. in debug mode your pointer is probably set to 0xcdcdcdcd by your compiler, unless you tell it differently. add some pointer math and you get your value.

Share this post


Link to post
Share on other sites
Quote:
Original post by Iccarus
Debugging it, it crashes on trying to set any of those variables through the second class but if I try and do it normally it works fine.


This is because the "this" pointer is not correctly initialized. I base this upon the fact that 0xcdcdcdd9 is 0xCDCDCDCD + 0xC (12). The former is a common debugging fill patern, and the later is a very sane offset.

Basically, you're doing something like:

ROOM * room; /* Room debug-initialized to 0xCDCDCDCD (in release mode it
* could be anything - it is officially uninitialized, and
* could contain old data in memory.
*/
room->SetType( 42 ); /* Room never assigned to an allocation, thus this is an
* error - if the type variable starts at the 13th byte
* of the class (an offset of 12 from the start) then an
* access violation @ 0xcdcdcdd9 is most likely.
* Turn on more warnings!!! Most compilers will catch
* this obvious a mistake - if yours dosn't, it's time
* to upgrade!!!
*/


The solution is to correctly allocate, or otherwise make valid the pointer:

ROOM foo;
ROOM * room; //room debug-initialized to 0xCDCDCDCD
room = & foo; //OK, room now set to a valid address
room->SetType( 42 );
room = new ROOM; //OK, room now set to a new, valid variable
room->SetType( 42 );
delete room;
room = 0; //sane "points to nothing" value

Share this post


Link to post
Share on other sites
Quote:
Original post by Iccarus
I can't get it to work in the way you described...


Assuming MAP::AddRooms() is the source of the crash, the problem is that roomlist[i-1] for i = ? is never assigned to a value. You probably want:
roomlist[i-1] = new ROOM;

Before the roomlist[i-1]->Set*(____) statements.

Quote:
Original post by jflanglois in an alternate thread of your triple post
Are you certain that length is correct? Your use of it seems a little awkward.


This is another good point. If you did assign all the roomlist[0..SIZE] to rooms, then the problem is likely that you're using roomlist[SIZE] or higher (note: an array of [SIZE] only contains the elements [0..(SIZE-1)] )

Share this post


Link to post
Share on other sites
That explains where all the other replies went, sorry bout that, dunno why it posted three times.

This is the entire code which makes up the maps:

//From map.h

// Map Class
class MAP
{
private:
int map;
string name;
int maxx;
int maxy;
vector<int> maparray;
ROOM* roomlist[255];
public:
void CreateFullMap(string newname,int newx, int newy,int* typearray,int newmap);
void AddRooms();
ROOM** GetRoomList();
ROOM* GetRoom(int number);
void SetMap(int number);
int GetMap();
int GetMapArrayElement(int element);
int GetMapArray();
int GetMaxX();
int GetMaxY();
void SetName(string newname);
string GetName();
};


//From map.cpp

#include "library.h"

void MAP::CreateFullMap(string newname,int newx, int newy, int* typearray, int newmap)
{
name = newname;
int length = (newx * newy) + 1;
maxx = newx;
maxy = newy;
map = newmap;
for (int a = 0; a < length; a++)
{
if (a == 0)
{
maparray.push_back(0);
}
else
{
maparray.push_back(typearray[a]);
}
}
AddRooms();
}

void MAP::AddRooms()
{
int length = (maxy * maxx) + 2;
for (int i = 1;i < length;i++)
{
int type = GetMapArrayElement(i);
GetRoom(i-1)->CreateMapRoom(type,i,i,GetMap());
}
}
ROOM* MAP::GetRoom(int number)
{
return(roomlist[number]);
}
ROOM** MAP::GetRoomList()
{
return(roomlist);
}
void MAP::SetMap(int number)
{
map = number;
}
int MAP::GetMap()
{
return(map);
}
void MAP::SetName(string newname)
{
name = newname;
}
string MAP::GetName()
{
return(name);
}
int MAP::GetMapArrayElement(int element)
{
return(maparray[element]);
}
int MAP::GetMaxX()
{
return(maxx);
}
int MAP::GetMaxY()
{
return(maxy);
}


//From room.h


// Room Class

class ROOM
{
private:
int type;
int map;
bool explored;
int x;
int y;
bool playerin;
public:
void CreateMapRoom(int tnumber, int newx, int newy, int map);
void SetPlayerin(bool value);
bool GetPlayerin();
void SetX(int xnumber);
void SetY(int ynumber);
int GetX();
int GetY();
int GetType();
int GetExplored();
void SetType(int typeno);
void SetExplored(bool value);
void SetMap(int mnumber);
int GetMap();
};


//from room.cpp

#include "library.h"

void ROOM::CreateMapRoom(int tnumber, int newx, int newy, int map)
{
SetX(newx);
SetY(newy);
SetType(tnumber);
SetExplored(false);
SetPlayerin(false);
SetMap(map);
}
void ROOM::SetX(int xnumber)
{
x = xnumber;
}
void ROOM::SetY(int ynumber)
{
y = ynumber;
}
int ROOM::GetX()
{
return(x);
}
int ROOM::GetY()
{
return(y);
}
int ROOM::GetExplored()
{

return(explored);
}
int ROOM::GetType()
{
return(type);
}
void ROOM::SetMap(int mnumber)
{
map = mnumber;
}
int ROOM::GetMap()
{
return(map);
}
void ROOM::SetExplored(bool value)
{
explored = value;
}
void ROOM::SetType(int typeno)
{
type = typeno;
}
void ROOM::SetPlayerin(bool value)
{
playerin = value;
}
bool ROOM::GetPlayerin()
{
return(playerin);
}


//from player.h

void AddMap(string newname, int newmaxx, int newmaxy, int* newtypearray, int mapno);
MAP* GetMapList(int mapno);


// from player.cpp

void PLAYER::AddMap(string newname, int newmaxx, int newmaxy, int* newtypearray, int mapno)
{

//check if duplicate
for (int check=0;check<20;check++)
{
if (maplist[check] != NULL)
{
//
if (maplist[check]->GetMap() == mapno)
{
//Exists already so do nothing
return;
}
}
}

//is not duplicate

for (int loop=0;loop<20;loop++)
{
if (maplist[loop] == NULL)
{
//Create map
maplist[loop] = new MAP;
maplist[loop]->CreateFullMap(newname,newmaxx,newmaxy,newtypearray,mapno);
//maplist[loop]->AddRooms();
return;
}
}

wait();
}
MAP* PLAYER::GetMapList(int mapno)
{
for (int mfind = 0;mfind < 20; mfind++)
{
if (maplist[mfind]->GetMap() == mapno)
{
return(maplist[mfind]);
}
}
return(maplist[0]);
}




I think thats everything.
If anyone can show me why its not liking reading out and writing in to the variables in room.h from player via map

Share this post


Link to post
Share on other sites
Quote:
Original post by Iccarus
That explains where all the other replies went, sorry bout that, dunno why it posted three times.

This is the entire code which makes up the maps:


Please use [ source ] tags in the future for large amounts of code. Doing a quick review... EDIT: You've fixed this - you may want to hit "edit" then "submit" again, so the forum will convert those HTML escapes for the carrots (<, >) back into their rightful ASCII codes. EDIT: Apparently you merged them too. That works :).

EDIT: The number of weird magic numbers like 2 make me suspect a problem there - but the fact that your code never contains the statement "new ROOM" makes me quite confidant that one of your problems is that you're never allocating any rooms.

Share this post


Link to post
Share on other sites
Quote:
Original post by Iccarus
Ok, thanks now it lets me write but not read.


You are reading after calling that function, right? You're probably attempting to read past the bounds of the ones you created/initialized/wrote to. See the quote of jflanglois I made ("Original post by..." text is bolded).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!