MSVC 2003 Debugging delete

This is a discussion on MSVC 2003 Debugging delete within the Windows Programming forums, part of the Platform Specific Boards category; I am not sure what I am doing incorrectly but when I step through with the debugger after selecting "delete ...

  1. #1
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396

    MSVC 2003 Debugging delete

    I am not sure what I am doing incorrectly but when I step through with the debugger after selecting "delete head" it still retains its original address:
    Code:
    ....
    void DeleteAllNodes()
    {
    	int test=0;
    	current =  head;
    
    	if(!head)
    		return;
    
    	if(WC.bDebug)
    	{
    		fprintf(fpDebug, "\nDeleteAllNodes from head to tail.\n");
    	}
    	while(1)
    	{
    		temp = head;
    		if(head->next == 0)
    			break;
    		head = head->next;
    
    		if(WC.bDebug)
    		{
    			fprintf(fpDebug, "Delete %4d current = %9p prev = %9p\n", test, temp, temp->prev);
    			fflush(fpDebug);
    		}
    		delete temp;
    		test+=1;
    	}
    
    	if(WC.bDebug)
    	{
    		fprintf(fpDebug, "Delete %4d head    = %9p prev = %9p\n", test, temp, temp->prev);
    		fflush(fpDebug);
    	}
    	delete head; 
    	return;  // head still has its original address!!!
    }
    The node head is not set to zero.

    This really screws me up in my next call because I use head to control my creation of the new linked list.
    Code:
    		if (! head )
    		{
    			head = new node;
    			tail = current = head;	// set current and tail = to head
    			current->next = 0;		// set next node to zero.
    			current->prev = 0;		// set prev node to zero or possibly set to tail!!!

    Do you see anything wrong with how I am calling delete?
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    It might still contain an address, but you'd be hosed if you tried to dereference it.

    delete head;
    head = NULL;

    If you want to be extra safe.

    It's the same with free() in C, free(p) does NOT set p to NULL.

    What about
    Code:
    while ( head != NULL ) {
      temp = head;
      head = head->next;
      delete temp;
    }
    Ensures head becomes NULL, and saves all the special case logic you have.

    And why does temp appear to be a global / class variable, and not a local to the function / block?
    And what does 'current' do ?
    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
    Registered User Bajanine's Avatar
    Join Date
    Dec 2001
    Location
    The most peaks over 10,000 feet!
    Posts
    396
    Well I'm bad, current is just hanging around waiting for something to do! Yeah I do think I need to clean this up a bit!
    Favorite Quote:

    >For that reason someone invented C++.
    BLASPHEMY! Begone from my C board, you foul lover of objects, before the gods of C cast you into the void as punishment for your weakness! There is no penance for saying such things in my presence. You are henceforth excommunicated. Never return to this house, filthy heretic!



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code completion troubles with MSVC 2003
    By Bajanine in forum Windows Programming
    Replies: 9
    Last Post: 08-21-2008, 06:43 PM
  2. MSVC 2003 break point problem
    By Bajanine in forum Windows Programming
    Replies: 6
    Last Post: 02-18-2008, 01:39 AM
  3. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. memory management...
    By master5001 in forum Game Programming
    Replies: 24
    Last Post: 01-07-2002, 04:50 PM

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