selective erase from associative container

hello, I use std::tr1::unordered_map and I want to erase all unreferenced resources from the map, to do so, I simply looked at their resource count if it is 1 I delete the resource and go to the next iterator, however deleting an iterator invalidates the iterator and trying to go to next one terminates the program with an assertion.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

void ResourceManager::removeUnreferancedResources() {

	ResourceMap::iterator i = mResourceMap.begin();
	ResourceMap::iterator e = mResourceMap.end();

	// loop trough resources and unload them
	for (; i != e; i++) {
		if (*i->second.getCount() == 1) {
			i->second->uninitialize();
			mResourceMap.erase(i++);
		}
	}

}


this one is worked

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


void ResourceManager::removeUnreferancedResources() {

	ResourceMap::iterator i = mResourceMap.begin();

	// loop trough resources and unload them
	for (; i != mResourceMap.end(); ) {
		// TODO: can we guaranty that this is the only place system
		// holds resource pointers for storage purpose ? Because
		// if not, count will be different than 1 
		// if there is only one reference which is it self, get rid of it
		if (*i->second.getCount() == 1) {
			i->second->uninitialize();
			mResourceMap.erase(i++);
		}else {
			++i;
		}
	}

}

Topic archived. No new replies allowed.