1

I am using a small library to have a database connection pool.

However, when I run the provided example, at the point where it calls DestroyPool I get a map is not increment-able assertion fail.

Here is the code

template <typename T>
int CommonDatabasePool<T>::DestroyPool(){
    typename map<T*, short>::iterator iter;
    for( iter = poolmap.begin(); !poolmap.empty()&& iter != poolmap.end(); iter++ ) {
        if(iter->second == DBPOOL_POOLEDCON_FREE){
            MyReleaseConcreteConnection(iter->first);
            poolmap.erase(iter);
        }
    }
    exitkeepalivepool = true;

    if(XSemPost(&semhandle)==-1){
    }
    if(XThreadJoin(&threadhandle)==-1){
    }

    return poolmap.size();
}

Is there anything here that looks suspicious that might cause such an error?

Whether or not I even use any connections it still does this...

Thanks

jmasterx
  • 52,639
  • 96
  • 311
  • 557

1 Answers1

3

Fundamentally, you are doing this code:

for(iter = poolmap.begin(); iter != poolmap.end(); iter++) {
    poolmap.erase(iter);
}

This code attempts to call iter++ on an iterator that is no longer valid.


You could fix this with something like:

typename map<T*, short>::iterator iter = poolmap.begin();
while (iter != poolmap.end()) {
    if (iter->second == DBPOOL_POOLEDCON_FREE) {
        MyReleaseConcreteConnection(iter->first);
        iter = poolmap.erase(iter);
    } else {
        iter++;
    }
}
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173