Thread: Setting a iterator's object to NULL

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    36

    Setting a iterator's object to NULL

    I created some test code to debug one of my problems, and I think I narrowed it down. One list has shared pointers, the other has addresses of the object pointed to by the shared pointer. When the shared pointer is deleted, it doesn't set the object to NULL so the list full of addresses has some bogus value. Here's some code to explain:

    Code:
    	list<shared_ptr<Enemy>> elist;
    	list<shared_ptr<Enemy>>::iterator it;
    	Enemy* ep;
    
    	{
    		shared_ptr<Enemy> e(new Enemy);
    		elist.push_back(e);
    		ep=e.get();
    	}
    	
    	it=elist.begin();
    	//THIS DOES NOT WORK, but indicates what I want: (*it)=NULL;
    	elist.erase(elist.begin());
            //this fails
    	assert(ep);
    How can I assign a new value to what an iterator is pointing to, or otherwise fix this?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    it->reset()

    But erasing should be enough. The destructor takes care of deleting the object.

    Code:
            //this fails
    	assert(ep);
    No, it won't. ep is a dangling but non-null pointer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    I'll give reset a shot when I get home, but yes, that assertion does fail (with the code I have posted now).

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by CornedBee View Post
    No, it won't. ep is a dangling but non-null pointer.
    QFT!
    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"

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    "This fails" or this "fails to fail"?
    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).

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    I guess I said that wrong. I WANT ep to be a NULL pointer, or somehow be able to tell when whatever ep is pointing to has been deleted.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just a guess, but wouldn't making ep a weak_ptr do help with that?

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    Quote Originally Posted by Daved View Post
    Just a guess, but wouldn't making ep a weak_ptr do help with that?
    Thanks a ton. That was exactly what I needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM