Hi,
here's my problem: given a huge source, where object pointers are stored in STL objects (queues, maps, deques, etc.), which would be the most easiest method to determine whether an a pointer (iterator) is really a valid one.
The problem is as follows: there are separate threads, which store many pointers to objects as described above. Due to the complexity of the project, sometimes occures that some of these pointers are deleted, but their values are not *yet* removed from the queues / maps / ... where they were inserted.
This leads in extreme cases - sporadically - to access violations, as if in this case the pointer is found in the STL objects, and unfortunately they pass checking for NULL pointers.
To illustrate the problem, here's a short example:
I thought of a method: whenever a pointer is extracted from an STL object, we try to read/write to the pointed memory location.Code:// deques which stores pointers of ClassA typedef std::deque< ClassA* > BasePtrList; BasePtrList myPtrList; // Thread_1 ClassA* myClassPtr = new(Class); // Thread_2 stores the pointer in his deque myPtrList.push_back( myClassPtr ); // later Thread_1 deletes aClassPtr, delete myClassPtr; myClassPtr = NULL; // step 1 /*issue command to remove this pointer from Thread2's myPtrList*/ maintainThread2List();step 2 /* after myClassPtr but before *before* the pointer is removed from Thread 2's deque - between step 1 and step 2 - Thread2 performs following action */ for ( BasePtrList::iterator anIter = myPtrList.begin(); anIter != myPtrList.end(); anIter++ ) { ClassA* aClassPtr = *anIter; // NULL pointer check if ( aClassPtr ) { aClassPtr->doSomething() } }
If there's any (catched) exception, we know that the pointer is not more valid, and it can be removed.
Maybe you've got the same problem. In case you have a better idea, you'd be welcome.
Thanks in advance!
Have a nice code.