calling exit() crashes

This is a discussion on calling exit() crashes within the C++ Programming forums, part of the General Programming Boards category; In my program I have this function: Code: void menuTestExit(Node* root) { printf("before delete\n"); delete root; printf("after delete\n"); exit(0); } ...

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    calling exit() crashes

    In my program I have this function:
    Code:
    void menuTestExit(Node* root) {
    	printf("before delete\n");
    	delete root;
    	printf("after delete\n");
    	exit(0);
    }
    I have to do it this way because I'm using glut. The problem is that this code always prints before delete and after delete, then creates a JIT debugging error (even when I run the executable directly). Is this something I should even be worried about, and if so, how do I fix it?
    Illusion and reality become impartiality and confidence.

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    Are you checking that root points somewhere before deleting it?

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  4. #4
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    Quote Originally Posted by sand_man
    Are you checking that root points somewhere before deleting it?
    Yes. It's a valid pointer. I also tried just commenting out the delete line, but it still gives the same error.

    Quote Originally Posted by anonytmouse
    The issue, if I'm understanding correctly, is that destructors don't get called implicitly when exit is called because the stack doesn't unroll, right? This is why I delete the root and kill all the data. I tried rewriting the exit function like this:
    Code:
    void menuTestExit(Node* root) {
    	printf("before delete\n");
    	delete root;
    	printf("after delete\n");
    //	exit(0);
    	throw(ExitExc());
    }
    where ExitExc is just an empty class like this:
    Code:
    class ExitExc {};
    Then I changed the top like this:
    Code:
    int main() {
    //some other stuff
    	try {
    		glutMainLoop();
    	} catch (ExitExc e) {
    	}
    	return 0;
    }
    But this gives the same error.
    Last edited by ichijoji; 12-19-2005 at 12:20 AM.
    Illusion and reality become impartiality and confidence.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    The problem is elsewhere in your code. Check your memory allocation/deallocation, buffer overruns, destructors, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling delete Crashes my Program
    By thetinman in forum C++ Programming
    Replies: 19
    Last Post: 10-13-2007, 04:07 AM
  2. MMX/SSE2 instrinsic crashes on function exit
    By jasonhv in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2006, 11:18 AM
  3. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 01:56 AM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 03:06 PM
  5. Calling exit() with dynamic memory
    By miclus in forum C Programming
    Replies: 11
    Last Post: 10-05-2004, 12:49 PM

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