Hi All,
So I'm trying to get back into C++ (mostly java for a while where memory management is avoided). So I've got this program and it runs perfectly when I don't try to clean up the memory. I don't want memory leaks so I have to clean up the memory.
Basically I have a class that represents the game board, each "space" on the board is a seperate class with a pointer to the next class (circular singly-linked list). All that works fine.
What I'm trying to get the AI to do is copy the game board for each possible move it can make. Then apply some changes to these copies, then analyze the results. The problem is that when I go to delete I get Access violations and I can't for the life of me figure out why, I've been at it for hours, just this one thing. Once it's fixed I'm essentially done. Without the calls to Delete it works, when I try to clean up that memory with them, it fails. Relevant code is below, please help!
The copy() function simply copies the Board object that calls it by creating a new Board with all the same values and then returns a pointer to this board.
I'm sure what I'm doing wrong is something stupid/obvious it's just been a long time since I've coded in C++ and pointers have always given me trouble.
This is called inside a function which is passed a pointer to CBoard, the function is inside another class that gets instantiated for the computer's decision making.Code:... Board * tests[6]; //Instantiate everything int nums[6]; for(int i=0; i<6; i++) { nums[i] = 0; tests[i] = CBoard->copy(); } ... Messes around with tests ... Done with all of them, we can delete them ... for(int i=0; i<6; i++) { delete tests[i]; } delete [] tests;
I've tried using vectors, and making Board **tests; then tests = new Board *[6]; Nothing seems to work.
The Board destructor works in when the main program loop deletes the main Board object, so the issue has to be with how I'm going about this.
Thanks!
EDIT:
Here's the error VC++ is throwing: 0xC0000005: Access violation reading location 0xfeeefee2
I looked to make sure that the copies of the new Boards were actually seperate/different/at-different-memory-locations than the original, and they appear to be. Which is where I'm just completely lost.