Thread: Pointer validity check

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463

    Pointer validity check

    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:
    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()
              } 
    }
    I thought of a method: whenever a pointer is extracted from an STL object, we try to read/write to the pointed memory location.
    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.
    Last edited by Carlos; 12-10-2003 at 05:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Another Linked List plee
    By Dragoncaster131 in forum C Programming
    Replies: 3
    Last Post: 05-15-2004, 05:40 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM