Thread: Problem deleting/freeing memory

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    USA
    Posts
    17

    Exclamation Problem deleting/freeing memory

    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.

    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;
    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.

    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.
    Last edited by DougD720; 10-27-2013 at 01:12 PM. Reason: Added Error

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    As written, this is wrong.

    delete [] tests;

    You only delete what you got from new.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the code that you tried using std::vector? It would help if you posted the smallest and simplest program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2011
    Location
    USA
    Posts
    17
    Thanks Salem,

    I removed that delete [] line, but still appear to be getting the same error. I think it has something to do with the underlying linked list contained in the Board class.

    This is where the debugger is pointing to after the error: dbgdel.cpp: /* verify block type */ _ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

    I think it's looking at a bad pointer... maybe it has to do with how I delete the linked list in the destructor?

    If that's any help:

    Code:
    ~Board()
    {
    
    	//Delete the boxes from last to first
    	for(int i=2; i<=6; i++)
    	{
    		Box * pBox = getPlayerBox(i);
    		Box * cBox = getComputerBox(i);
    		delete pBox;
    		delete cBox;
    	}
    
    	delete firstPlayerBox;
    	delete firstComputerBox;
    }
    But like I said when I run this after the main game board is done with in the main program I don't get any issues.

    laserlight:

    Unfortunately I've never worked with vectors before, so I gave up after trying them and don't have that code anymore. There's got to be a way to do this with pointers.
    Last edited by DougD720; 10-27-2013 at 01:34 PM.

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    If the bug is still present, it is possible that something is wrong with CBoard->copy(). But noone will help you unless you provide a small, compilable example.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Location
    USA
    Posts
    17
    Copy method
    Code:
    Board * copy()
    {
    	Board * ret = new Board(0);
    
    	for(int i=1; i<=6; i++)
    	{
    		ret->getComputerBox(i)->add(getValueComputerBox(i));
    		ret->getPlayerBox(i)->add(getValuePlayerBox(i));
    	}
    
    	return ret;
    }

  7. #7
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You have to provide something that compiles, has main(), and can be copy-pasted... and is small which means that you have to keep only the relevant (bugged) part.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If your program is less than 300 actual code lines you can post the whole shebang. Perusing your snippets, I suspect there's other errors in it anyway. Or you could take out all the AI, for instance, as long as it still demonstrates the error.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Mar 2011
    Location
    USA
    Posts
    17
    Thanks for the help guys, Salem was in fact right (no surprise) but the reason it wasn't working wasn't because of anything more than I was missing a <= in one of the for loops in the constructor, (I had a < only). Thus one of the objects wasn't being initialized, thus when I went to delete it, there was nothing to delete and thus the error. One of those true DOH! moments. Thanks for all the help!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A little late perhaps, but consider not using pointers at all. Makes copies of all your objects. It's much easier than your current method.
    If you need polymorphism, then consider using smart pointers (shared_ptr, unique_ptr, weak_ptr).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing memory
    By Soel in forum C Programming
    Replies: 10
    Last Post: 12-16-2010, 06:31 AM
  2. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  3. Freeing memory
    By vbdave78 in forum C Programming
    Replies: 7
    Last Post: 11-18-2009, 12:56 PM
  4. SIGSEGV, memory allocation/freeing problem
    By symf in forum C Programming
    Replies: 4
    Last Post: 02-06-2009, 07:26 PM
  5. Freeing memory necessary?
    By Snip in forum C Programming
    Replies: 3
    Last Post: 11-05-2005, 07:01 AM