# Odd deletion problem

This topic is 4984 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello, ok this makes no sense (well to me anyways). ok i have a class that checks to see if objects are alive and then deletes them if they are dead. when the program closes it deletes all objects that are still left in that list. the destructor is as follows. for(list<IPollingObject*>::iterator i = m_listObjects.begin(); i != m_listObjects.end(); i++) { delete *i; } ok if no objects withen the course of the program are deleted the destructor runs fine, but if any object is removed (which it does becasue the list size reduces) i get a run time error on exit pointing me to the line "delete *i" ok but if i change the destructor to this ... list<IPollingObject*>::iterator i = m_listObjects.begin(); while (i != m_listObjects.end()) { delete (*i); i++; } i get a runtime error again but it points me here in a header called xmemory void deallocate(pointer _Ptr, size_type) { // deallocate object at _Ptr, ignore size ::operator delete(_Ptr); } points to the ::operator delete(_Ptr); what is going on here ??? im using ms visual studio express. thanks for any help. EDIT: i should thank leiavoia for pointing me in the right direction on a previous post.

##### Share on other sites
Did you remove the item from the list? Sure doesn't look that way. Which would mean you are attempting to delete an object that has already been deleted...

##### Share on other sites
Yup, looks like you need to call erase after you delete it, or at least null the pointer after deletion. Deleting NULL is harmless IIRC.

##### Share on other sites
i'm pretty sure the objects are being deleted. for instance objects that can be drawn are tested before each render and objects which return false on poll are removed from that list.

then at the end of each frame, each object that returns false on poll is actually deleted.

this is the actual code ...

EDIT: the interface ISceneNode derives from IPollingObject

void CScene::render() {	for (uint_t n = 0; n < m_treeObjects.size(); n++) {		list<ISceneNode*>::iterator i = m_treeObjects[n].begin();		while (i != m_treeObjects[n].end()) {			if ((*i)->poll()) {				(*i)->draw(m_bmpBuffer);				i++;			}			else {				i = m_treeObjects[n].erase(i);			}		}	}}

void CPollingDevice::update() {	list<IPollingObject*>::iterator i = m_listObjects.begin();	while (i != m_listObjects.end()) {		if ((*i)->poll()) {			i++;		}		else {			delete *i;			i = m_listObjects.erase(i);		}	}}

basically im testing so when i press the space bar an object will return false on the next poll (being immidatly after the key was pressed in the same frame)

when i actually remove the object in the program by hitting space bar it is removed from the scene, and the list size of the poller goes down, and the list size of the scene goes down, so the lists for each system the object belongs to is affected.

so i really don't know what is going on, becasue usually if i try and delete something that isnt there it gives me a segmentation fault.

##### Share on other sites
So basically, you've got the same item in multiple lists? That could get scary later on. Do you think there is any chance you could consolidate your lists?

Failing that, you just need to be very careful to iterate your lists in very specific order. I can't see anything else to fix.

However, i did notice that your two lists do different functions. It looks like one draws and the other cleans up. Correct? Is there a chance you could clean up as you go? like

if (condition) { do something } else { kill'em }

If you can do both at once, you don't have to iterate over the objects again. The original code i posted does this (i can tell you looked at it, then bastardized it with your Hungarian Notation ;-P ) My first draft of my object processor tried to do it in two passes and i cleaned it up so it works in one, like above.

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
633000
• Total Posts
3009815
• ### Who's Online (See full list)

There are no registered users currently online

×