help with unhandled exception

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?

Any help is welcome.

Cheers,
Dorien
It's program error. You'll have to post the relevant part of your code.
This is the part that gives the exception:

1
2
3
4
5
6
for (j=0; j<2; j++) {
			Loc where1 = Where(via, onto) + 1;
			if (AtBottom(via, onto) && OkayToMove(via, moved, where1)) {
				TryMove(via, moved, where1, MV_TFULL);
				intab |= (1 << i);
			}
We'll need to see some more code than that. As a starting point, we could need to see the functions Where, AtButtom, OkayToMove and TryMove.

Before you show us the code though, perhaps you could use your debugger to work out exactly which line is causing a problem.
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();
}


This is the last part of code that it executed.

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.
This is the upper line of the call stack:
test.exe!DFS(pos_struct * via)  Line 3761 + 0x18 bytes	

If I doubleclick it, it jumps to this part of the code:

TryMove(via, moved, where1, MV_TFULL)

The second line of the call stack :
test.exe!Search(int permuted)  Line 3936 + 0x11 bytes	



If I doubleclick this it jumps to this part of the code:
if (success == IMPOSSIBLE /* i.e., 0 */) DFS(pos0);

This is the prototype of the DFS function:
static void DFS(Position *pos);

This is the DFS function:
static void DFS(Position *via)

Does this help?
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
static void 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.

Does anyone now how I can solve this problem?
malloc returns null if it can't get enough memory. Either require less memory, or increase the amount of memory the OS has to hand out.

Also, I'd put this in once
cout << sizeof(Position);
to check that you're not asking for some utterly ridiculous amount of memory.
Reading pos_struct make me think that it does not initialize it's members (like deeper) and so deeper is always non null.
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
typedef struct 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.

Can anyone help me with this?
when creating a variable of type Position always set it to zero, like so:
1
2
3
4
5
Position *pos = (Position *)malloc(sizeof(Position));
memset(pos, 0, sizeof(Position));
...
via->deeper = (Position *)malloc(sizeof(Position));
memset(via->deeper, 0, sizeof(Position));

This way you will ever have reliable values.

See: http://cplusplus.com/reference/clibrary/cstring/memset/
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.
I found the problem: I accidentally changed 1 line into comment. This line happened to be the initialisation of 1 of the objects.

Thanks for all of your help!
Topic archived. No new replies allowed.