Like Tree1Likes
  • 1 Post By kmdv

Problem deleting/freeing memory

This is a discussion on Problem deleting/freeing memory within the C++ Programming forums, part of the General Programming Boards category; Hi All, So I'm trying to get back into C++ (mostly java for a while where memory management is avoided). ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,986
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    682
    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.
    I never put signature, but I decided to make an exception.

  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
    682
    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.
    stahta01 likes this.
    I never put signature, but I decided to make an exception.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,820
    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, 05: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, 11:56 AM
  4. SIGSEGV, memory allocation/freeing problem
    By symf in forum C Programming
    Replies: 4
    Last Post: 02-06-2009, 06:26 PM
  5. Freeing memory necessary?
    By Snip in forum C Programming
    Replies: 3
    Last Post: 11-05-2005, 06:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21