Thread: Pointer validity check

  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.

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    An idea off on a slight tangent -

    You don't want to recode, but you may want to consider smart pointers from the boost library, since you are going to have to change at least some of your code anyway. Smart pointers excel at solving this kind of problem.

    www.booost.org

    I have not used them in the Windows environment, but others have.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You've got one too many 'o' s in your link, Jim.

    http://www.boost.org
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    This sounds as if you need syncronization in your threads.

    Have a look at Critical Sections, semaphores, mutexes and locks in general. You need to make sure that no other thread can do stuff between step 1 and 2.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by nvoigt
    This sounds as if you need syncronization in your threads.

    Have a look at Critical Sections, semaphores, mutexes and locks in general. You need to make sure that no other thread can do stuff between step 1 and 2.
    The code was just a rough approach of what happens.
    The stuff is more sophisticated. The critical sections are of course guarded(using locks).
    Last edited by Carlos; 12-10-2003 at 07:22 AM.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Allocate and de-allocate your objects from a "memory pool" object instead of the heap. Then you can query the memory pool object: "Is this pointer still out, or has it been free'd?"

    You'll need to make the memory pool object thread safe of course.

    Best suggestion I can give without knowing all the gory details

    gg

  7. #7
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by Codeplug
    Allocate and de-allocate your objects from a "memory pool" object instead of the heap. Then you can query the memory pool object: "Is this pointer still out, or has it been free'd?"
    gg
    Good suggestion, CodePlug. I think this one would take some more extra work than my approach (pointer validity check), but it's also more elegant.

    Thank you.

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