Application freezes up when delete this is called.

This is a discussion on Application freezes up when delete this is called. within the C++ Programming forums, part of the General Programming Boards category; This is the function that causes the problem. Code: Deck::~Deck() { delete this; } I call the function in this ...

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964

    Application freezes up when delete this is called.

    This is the function that causes the problem.

    Code:
    Deck::~Deck()
    {
    	delete this;
    }
    I call the function in this function:
    Code:
    void Card_Game::cleanup()
    {
    	
    	deck->~Deck(); // delete deck
    
    	for (unsigned int loop = 0; loop < players.size(); loop++) // delete hands
    	{
    		players[loop].clear_hand();
    	}
    
    	players.clear(); //delete players
    	
    }
    And I call cleanup here:

    Code:
    int Card_Game::main_menu(int result)
    {
    	switch(result)
    	{
    	case 1: // play blackjack
    		cleanup(); // cleanup just in case
    		initialize_players(2);
    		deck = new Deck;
    		deck->shuffle_deck();
    		players[0].draw(*(deck), 1);
    		players[1].draw(*(deck), 1);
    		return 1;
    	case 2: // quit game
    		return 3;
    	default:// invalid entry
    		std::cout << "Please enter 1 or 2.\n";
    		return 0;
    	}
    }
    Basically the program freezes when I make it go to case 1. Any ideas why I can't do that?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    I can't say I know why you can't do that, I do know it isn't a good idea though. If someone decides not to dynamically cast a deck than it would cause problems. Might as well just delete it instead of calling the destructor.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Quote Originally Posted by Drac View Post
    I can't say I know why you can't do that, I do know it isn't a good idea though. If someone decides not to dynamically cast a deck than it would cause problems. Might as well just delete it instead of calling the destructor.

    Hmm, I switched it to delete deck, but that still causes my program to freeze, maybe it's beacuse I didn't initialize a deck yet?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Why not use the common routine: Deck does not delete itself in the destructor and the user is responsible for calling delete for an object that is allocated with new.

    Your problem is probably infinite recursion. You call the destructor explicitly (very bad idea in 99.99&#37; cases), destructor calls delete, which in turn calls the destructor etc.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Okay, so I do this:

    Code:
    void Card_Game::cleanup()
    {
    	delete deck;
    
    	for (unsigned int loop = 0; loop < players.size(); loop++) // delete hands
    	{
    		players[loop].clear_hand();
    	}
    
    	players.clear(); //delete players
    }
    And it is still freezing up for some reason on the delete call.

    deck is a Deck*, does deck need to actually point to something for me to call delete on it?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    deck is a Deck*, does deck need to actually point to something for me to call delete on it?
    Yes, or it is a null pointer.
    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

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Doing "delete this" also assumes that the object is created by new, and inside the object, you can't guarantee that such is the case, e.g.
    Code:
    void func()
    {
       Deck d;
       ... Some code
    }
    Doing delete this on the above code would DEFINITELY cause badness - how bad depends on the particular compiler - if you are lucky, it gives you an indication such as "bad heap in free", but if you are not so lucky, things will just go completely and horribly strange until it crashes on some future memory allocate/free operation (new/delete or malloc/free) that has a "wild pointer". These type of problems can be hard to find.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Okay, now I'm even checking to see if deck is not null before i try to delete it, but the program is still crashing when i go to case 1...

    Code:
    void Card_Game::cleanup()
    {
    	if( deck != NULL)
    	{
    		delete deck;
    	}
    
    	for (unsigned int loop = 0; loop < players.size(); loop++) // delete hands
    	{
    		players[loop].clear_hand();
    	}
    
    	players.clear(); //delete players
    }
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is only OK to delete something if the pointer actually points to an object allocated with new or is set to 0. Unless you pick one of the cases, the pointer will be uninitialized, containing a garbage address which isn't necessarily 0.

    You need to initialize your variables, preferably in the constructor.

    [Edit]
    Another idea. Does deck absolutely have to be allocated dynamically? Perhaps it could simply have a reset member function that you could call at any time without destroying and recreating it all the time?
    Last edited by anon; 01-09-2008 at 09:12 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Well, I would like to allocate the deck dynamically, I'm trying to keep it so i only pool resources when I need them, so when I'm in the main menu I don't need any deck instances or player instances running or anything like that.

    why is my null check not working? It's still calling delete on the null pointer and crashing the program :\

    The Deck* is a member of the Card_Game class...

    but calling delete on deck should delete the memory pointed to not the pointer right?
    Last edited by Shamino; 01-09-2008 at 09:23 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    why is my null check not working? It's still calling delete on the null pointer and crashing the program
    As I noted, it is safe to use delete on a null pointer. It is not safe to use delete if the pointer has not been initialised, or is not null and yet the object it points to no longer exists.
    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

  12. #12
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    oohhh, how do I check then if the pointer has been initialized?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  13. #13
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    >> players[0].draw(*(deck), 1);
    Unless draw() takes a Deck reference, that will create a copy on the stack - which won't delete very well.

    gg

  14. #14
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Draw does indeed take a reference

    Code:
    void Player::draw(Deck & deck, int number)
    {
    	for(int loop = 0; loop < number; loop++)
    	{
    		hand->card_list.push_back(deck.cards.back());
    		deck.cards.pop_back();
    	}
    }
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by Shamino View Post
    oohhh, how do I check then if the pointer has been initialized?
    As far as I know you can't check it. You ensure that there are no uninitialized variables in the code by initializing everything at the point of declaration or in constructors.

    Code:
    int* p; //bad
    int*q = 0; //good
    int* r = new int; //also good
    
    MyClass() {} //bad
    MyOtherClass(): my_member_pointer(0) {} //good
    MyThirdClass(): my_member_pointer(new Something) {} //also good
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can we debug into delete
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2007, 04:45 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. why delete[] and delete?
    By dude543 in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2005, 10:55 PM
  4. delete on a NULL pointer
    By myname in forum C++ Programming
    Replies: 18
    Last Post: 10-03-2003, 08:23 PM
  5. Delete Item
    By emilyh in forum Windows Programming
    Replies: 10
    Last Post: 10-03-2001, 09:33 PM

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