Thread: Destructor not being Called

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    19

    Destructor not being Called

    Hi,

    Pseudocode of architecture:
    Code:
    class class2 {
        private:
            float* class_array;
        public:
            class2() { class_array = new float[10]; }
            ~class2() { delete [] class_array; }
    }
    class class1 {
        private:
            class2 my_class;
            int* unimportant_array;
        public:
            class1() {
                unimportant_array = new int[4];
            }
            ~class1() {
                delete [] unimportant_array;
            }
    }
    int main() {
        //...
        class1* var = new class1();
        //...
        delete var;
        //...
    }
    For some reason, I'm getting a memory leak. Using breakpoints, I've determined that the destructor of "class2" isn't called, and therefore "class_array" isn't getting delete-ed. But, when "class1" is destructed, shouldn't "my_class" also be destructed? Ideas?

    Thanks,
    Ian
    Last edited by Geometrian; 05-01-2011 at 12:07 PM. Reason: error in pseudocode

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    You should get a compiler error because var needs to be a pointer to a class1 object.

    Code:
    class1 *var = new class1();
    As far as i know, my_class should be deleted too, when you delete an object of class1.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Quote Originally Posted by threahdead View Post
    You should get a compiler error because var needs to be a pointer to a class1 object.
    Mmm yep. Pseudocode error. Fixed.
    Quote Originally Posted by threahdead View Post
    As far as i know, my_class should be deleted too, when you delete an object of class1.
    That's what I was thinking. So why isn't it?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    If you are having trouble with a bit of code, post that code! Let's not pretend...

    If your code is as you posted, with the addition of the missing simicolon, and the destructor really isn't being called, the compiler has a bug.

    That is extremely unlikely, or in other words, the problem is in your real code.

    Soma

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Well, alrighty. I'm gonna post the minimum required to understand the code. Here goes.

    glLibView.h/glLibView.cpp:
    Code:
    class glLibInternal_glLibView {
    	private:
    		glLibMathMatrix<float>* _matrix;
    	public:
    		//...
    		
    	public:
    		glLibInternal_glLibView(void);
    		glLibInternal_glLibView(int r[4], float near_value, float far_value);
    		~glLibInternal_glLibView(void);
    	private:
    		void glLibInternal_copy_from( const glLibInternal_glLibView* copy);
    	//...
    };
    glLibInternal_glLibView::glLibInternal_glLibView(void) {
    	_matrix = new glLibMathMatrix<float>(4,4);
    }
    glLibInternal_glLibView::glLibInternal_glLibView(int r[4], float near_value, float far_value) {
    	_matrix = new glLibMathMatrix<float>(4,4);
    	//...
    }
    void glLibInternal_glLibView::glLibInternal_copy_from( const glLibInternal_glLibView* copy ) {
    	glLibMathMatrix<float>::copy(_matrix,copy->_matrix);
    	//...
    }
    glLibInternal_glLibView::~glLibInternal_glLibView(void) {
    	delete _matrix;
    }
    
    
    class glLibView2D : public glLibInternal_glLibView {
    	//...
    };
    glLibView2D::glLibView2D(void) : glLibInternal_glLibView() {}
    
    
    class glLibView3D : public glLibInternal_glLibView {
    	//...
    };
    glLibView3D::glLibView3D(void) : glLibInternal_glLibView() {}
    
    
    class glLibViewISO : public glLibInternal_glLibView {
    	//...
    };
    glLibViewISO::glLibViewISO(void) : glLibInternal_glLibView() {}
    _matrix is the leaking variable.

    tutorial.h:
    Code:
    class tutorial {
    	public:
    		//...
    		glLibClock* clock;
    	//...
    
    	public:
    		tutorial(void) {
    			clock = new glLibClock(20);
    			//...
    		}
    		~tutorial(void) {
    			delete clock;
    		}
    		//...
    };
    ext_ray_trace.h:
    Code:
    class tutorial__ext_ray_trace : public tutorial {
    	public:
    		glLibView3D View3D;
    		//...
    
    	public:
    		tutorial__ext_ray_trace(void) {}
    		//...
    };
    tutorial__ext_ray_trace has an implicit destructor. View3D's destructor is not being called, and so View3D._matrix is leaking.

    Tutorials.cpp:
    Code:
    void RunTutorial(tutorial* t) {
    	//do some stuff to run t
    	delete t;
    	//...
    }
    int GetInput(void) {
    	SDL_Event event;
    	while (SDL_PollEvent(&event)) {
    		switch (event.type) {
    			case SDL_KEYDOWN:
    				switch (event.key.keysym.sym) {
    					//...
    					case SDLK_k: RunTutorial(new tutorial__ext_ray_trace()); break;
    				}
    			//...
    		}
    	}
    	return -1;
    }
    int main(int argc, char **argv) {
    	//...
    	while (true) {
    		status = GetInput();
    		//...
    	}
    	//...
    }
    The full sources of these files is can be found here.

    Thanks,
    Ian
    Last edited by Geometrian; 05-01-2011 at 12:56 PM.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I'm assuming virtual deletion without a virtual destructor.

    Soma

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    And if it's not that then my bet is that it's failure to follow the rule of three.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    127
    Code:
    void RunTutorial(tutorial* t) {
    	//do some stuff to run t
    	delete t;
    	//...
    }
    To delete object of tutorial__ext_ray_trace correctly, you should declare the destructor of class tutorial as a virtual destructor.
    Nana C++ Library is a GUI framework that designed to be C++ style, cross-platform and easy-to-use.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    19
    Thanks. I see why that's necessary now; deleting via a pointer to the base class shouldn't work as expected--in any case, I wouldn't expect the derived class's destructor to be called.

    Thanks again!
    Ian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. destructor not called?
    By waysgoose in forum C++ Programming
    Replies: 3
    Last Post: 04-11-2010, 01:08 AM
  2. Why would the destructor be called?
    By therabidwombat in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2009, 12:09 PM
  3. Why the destructor is called?
    By scherzo in forum C++ Programming
    Replies: 5
    Last Post: 10-23-2007, 05:11 AM
  4. Why is destructor being called??
    By Bill83 in forum C++ Programming
    Replies: 31
    Last Post: 03-21-2006, 09:49 PM
  5. Why is destructor being called??
    By Bill83 in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 06:16 AM