Hi,
I'm working with a code that I found online. After I resolved all the errors I tried to run the program, but it keeps giving me an unhandled exception.
This is everything that is standing in the output window:
'test.exe': Loaded 'C:\Users\dorien\Documents\school\masterproef\freecell\solver\test\Debug\test.exe', Symbols loaded.
'test.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'test.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'test.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'test.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
First-chance exception at 0x011b68b0 in test.exe: 0xC0000005: Access violation reading location 0xcdcdcedd.
Unhandled exception at 0x76f415de in test.exe: 0xC0000005: Access violation reading location 0xcdcdcedd.
The program '[3092] test.exe: Native' has exited with code -1073741819 (0xc0000005).
Does anyone know what the problem is?
Or what is located at reading location 0xcdcdcedd?
I debugged the code again and it gives the same error message, but now it stops in crtexe.c at this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void
)
{
/*
* The /GS security cookie must be initialized before any exception
* handling targetting the current image is registered. No function
* using exception handling can be called in the current image until
* after __security_init_cookie has been called.
*/
__security_init_cookie();
return __tmainCRTStartup();
}
You need the program to stop in the debugger. Once that's happened, look at the call stack, you should recognise some part of your program very near the top of the call stack.
If the an exception has been thrown, then you will loose that context. But we don't think that's what's happening here.
The value 0xCDCDCDCD is used by Microsoft's C++ debugging runtime library to mark uninitialised heap memory. You are trying to read a location suspiciously similar to that, which suggests to me that you are interpreting uninitialised heap memory as a pointer, carrying out some kind of pointer arithmetic (adding 0x110, or 272 in decimal), and then trying to dereference the pointer, which now has the value 0xcdcdcedd - and then the OS stops you from reading that location, as it should.
If you build with debug symbols included, you'll get more information. Ideally, you should debug the code and interrogate the vale of the parameters being used at the bad line - one or more of them is a bad value.
I figured out where the error is located, it's in the TryMove function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
staticvoid TryMove(Position *via, Card card, Loc whither, int whatType)
/* Position *via; Card card; Loc whither; int whatType;*/
{
int dup = false;
int atEntry;
Position *pos;
Position *temp;
depth++;
if (via->deeper == NULL) {
via->deeper = (Position *)malloc(sizeof(Position));
via->deeper->deeper = NULL;
}
pos = via->deeper;
temp = pos->deeper;
The program stops while trying to perform the last line.
I think that the malloc function returns a null pointer and than pos is also turned into a null pointer. So the program crashes when it tries to use pos as a pointer.
Also, I'd put this in once
cout << sizeof(Position);
to check that you're not asking for some utterly ridiculous amount of memory.
I've checked the size of Position and this is 276. So I don't think that this is the cause of my problem.
Reading pos_struct make me think that it does not initialize it's members (like deeper) and so deeper is always non null.
This is the code of pos_struct:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
typedefstruct pos_struct {
uchar foundations[4];
Card hold[4];
uchar colSeq[8]; /* sorted order for tableau columns */
Column tableau[8];
Loc location[52];
Move how; /* move that got us here */
uchar dontMoveTo; /* bitmap: cols that must shrink b4 growing */
uchar dontMoveFrom; /* bitmap: cols that must grow b4 shrinking */
uchar goodcols; /* columns w/ potential forced found'n moves */
long swappable; /* bitmap of swappable pairs */
long swapped; /* pairs that have been swapped */
struct pos_struct *via; /* position from which this was reached */
TreeEntry *tree; /* hash table entry for this position */
struct pos_struct *deeper; /* where to build next deeper position */
} Position;
I think that there is something wrong with via and deeper. While debugging they both have things like bad pointer standing in the locals window.
I've never worked with struct before, so I don't really know how to solve this.
I've changed the code like you said, but this doesn't solve the error.
Only pos changes: it goes from 0xcccccccc to 0x00564c80.
But via -> deeper stays 0xcdcdcdcd.
So after this code: pos = via->deeper;
pos is also changed into 0xcdcdcdcd.
The problem is that pos->via, pos->deeper, via->via and via->deeper are wrong. They either have the value 0x00000000 or 0xcdcdcdcd.
I don't know how to solve this, all the rest of the struct pos_struct is loading fine.
The problem is that there are more uninitialzed objects. You know that when you see 0xcdcdcdcd. The compiler fills (in debug mode) an uninitialzed variable with 0xcdcdcdcd.
You need to find all places where objects are created and not set to 0. That's the problem when a struct/class doesn't initialize it's members.
When you delete/free such an object you must make sure that all variables that contain a pointer to this object (i.e. via/deeper) are set to NULL.