Thread: I always have memory errors in destructors

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    I always have memory errors in destructors

    Crikey. Whenever I allocate memory off the heap and delete it in the destructor, my program crashes. A la:

    Code:
    num_steps = 2;
    ai = new aistep [num_steps];
    
    /* .... */
    
    enemy::~enemy ()
    {
    	if (ai) {
    		if (num_steps > 1)
    			delete[] ai;
    		else
    			delete ai;
    		
    		ai = NULL;
    	}
    }
    Now if I comment all that code in the destructor out, the program starts and ends fine. No matter how I do it (no num_steps check , no if (ai) check, no ai = NULL, etc) it always crashes. And it does thise for other things too, not just classes but for basic types too. Anything allocated dynamically works, but deleting it and the whole shebang crashes.

    What am I doing wrong? Am I not supposed to delete stuff in the destructor? Is dynamically allocated memory deleted automatically in destructors? (I didn't think it was...)

    Thanks!
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    given your code you should always use delete []. Calling ordinary delete even on somthing allocated with new foo[1] is undefined, in this case a compiler vendor would have to go fairly far out of thier way to define it as anything other than works correctly, but we must always obey the holy standard. Secondly, if you need to write a destructor you almost always need to write a copy constructor and assignment operator. This is almost certainly your bug. Somewhere you have a copy of your enemy object that when it gets destroyed rips the guts out of your original object. You need to ether make your copy ctor and assignment operator private, and have them throw something just to make absolutely sure you aren't creating any temporaries in any members or friends. Or write a copy ctor that performs the deep copy. Naturally the prefered and obvious solution is to use std::vector.

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Heh

    I should've just use vector in the first place. What was I thinkin.

    Thanks, problem solved
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM
  5. True Errors in Memory and Storage
    By Procyon in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 02-07-2002, 07:31 AM