• 12
• 27
• 9
• 9
• 20

Avoiding this access violation (C++)

This topic is 2804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

So it looks like this (simplified to the relevant parts)

class Game{   ...   World* World_Ptr;   Distant* Distant_Ptr;   ...   Game();   ...};Game::Game(){   ...   World_Ptr = new World( <params> );   ...   Distant_Ptr = new Distant( <params>, *this);}Distant::Distant( <params>, Game& the_game ){   Game_Ptr = &the_game;   ...   SetAllMaps();}void Distant::SetAllMaps(){   clipmaps[F].SetPtrs(*Game_Ptr);   clipmaps[F].setMeta(F);   ...}void ClipMap::SetPtrs(Game& the_game){	Game_Ptr = &the_game;	Distant_Ptr = the_game.Distant_Ptr;}void ClipMap::setMeta(int F){   level = F;   unit = (int)pow(2.0f, F);   unit_secsize = unit*Game_Ptr->World_Ptr->secsize;   half_unit_secsize = unit_secsize/2;   side = unit_secsize*Distant_Ptr->size2n;   width = (int)pow(2.0f, Distant_Ptr->n-2);}

When my code gets to the bold I get an access violation.

Is it because World and Distant do not use default constructors so the Game_Ptr points to an invalid object? The Game constructor function is still on the stack at the point of the error as this is part of constructing the Distant object as you can see.

edit: bolded wrong line, fixed

Share on other sites
Thanks for your attempt at striping out the unnecessary code. Unfortunately there isn't enough left for the problem to show itself!

Please don't only report that it gave an access violation. These also give you more information that although you have not learned to use yet, the rest of us have. For instance it tells us whether it was reading or writing (I suspect "reading" here), and to what address the violation occurred at. Always give the full message including what to you looks like a seemingly non-meaningful memory address. To the trained eye it is often most useful.

You're also describing stuff about the callstack. You have this very useful information and yet can't diagnose the problem. If you don't want to make this even harder for us than it is currently for you, then you'll need to post the callstack at the time of the crash as well. In Visual Studio you can copy and paste the callstack out as text. As for any other debuggers that is also likely but I'm not sure. Worst case you might just need to upload a screenshot of it.

Share on other sites
Ah, yeah it is a reading violation.

I'm using VC Express and the error window says:

Unhandled exception at 0x00411e42 in SDLGL12.exe: 0xC0000005:Access violation reading location 0xccccccd8.

stack looks like:
>	SDLGL12.exe!ClipMap::setMeta(int F=0)  Line 14 + 0xc bytes	C++ 	SDLGL12.exe!Distant::SetMap(int F=0)  Line 980	C++ 	SDLGL12.exe!Distant::SetAllMaps()  Line 973	C++ 	SDLGL12.exe!Distant::Distant(int n_in=6, int depth_in=10, Game & the_game={...})  Line 966	C++ 	SDLGL12.exe!Game::Game()  Line 1054 + 0x3a bytes	C++ 	SDLGL12.exe!SDL_main(int argc=1, char * * argv=0x003b5fb8)  Line 24	C++ 	SDLGL12.exe!_main()  + 0xf5 bytes	C 	SDLGL12.exe!__tmainCRTStartup()  Line 586 + 0x19 bytes	C 	SDLGL12.exe!mainCRTStartup()  Line 403	C 	kernel32.dll!7c817057() 	 	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]

Share on other sites
You are most likely passing an uninitialized pointer, since the address 0xccccccd8 is very close to 0xCCCCCCCC which is the value of stack variables that you have not initialized (at least under visual studio in debug mode).
You should try stepping through your code and watch those pointers closely, I bet they are not initialized when you try to dereference it.

Quote:
 msdnSpecial Values:0xFDFDFDFD No man's land (normally outside of a process)0xDDDDDDDD Freed memory0xCDCDCDCD Uninitialized (global)0xCCCCCCCC Uninitialized locals (on the stack)

Share on other sites
I moved this bit of code out of the initialization for the pointed to objects and it worked :)

The only thing is I had to add an extra line of code here which I don't really like doing:
Distant_Ptr = new Distant(mapsize, levels, *this);	Distant_Ptr->SetAllMaps();

I would rather the SetAllMaps() function was called in the constructor

Share on other sites
Quote:
 Original post by PrestoChungI moved this bit of code out of the initialization for the pointed to objects and it worked :)The only thing is I had to add an extra line of code here which I don't really like doing:Distant_Ptr = new Distant(mapsize, levels, *this); Distant_Ptr->SetAllMaps();I would rather the SetAllMaps() function was called in the constructor

You are still constructing the object at the constructor point so "this" doesn't need to be fully initialised or valid yet, this is also the reason you don't call virtual functions in the constructor.

After object construction(after the constructor is done) the this pointer in a class is available.