Saturday, March 29, 2008

defect in microsoft stl queue documentation

I've just spent the last four or five hours trying to work out why my asynchronous tasking code doesn't work.

After a while it turned out that the last element in my queue was being mangled when I was freeing the memory associated with the current work item. This could be one of two things:

  • I'm not popping the element of the back of the queue or,
  • Somehow I've mixed my pointers up while adding them to the queue and more than one queued task is looking at the same memory. The code that adds elements to the queue is a different thread to the one that handled it.

I looked at the intellisense for the pop command on the std::queue. "erase element at end" is what it says. So I thought, it must be something to do with thread-safety. Spend a bit more time writing down memory addresses to verify my code is logically correct. Nope, nothing wrong with what I'm doing. Just on the off chance I open up the sql queue code and have a look at the pop method and this little gem is what I found:

void push(const value_type& _Val)
{ // insert element at beginning
c.push_back(_Val);
}

void pop()
{ // erase element at end
c.pop_front();
}

A couple of lines of incorrect commenting in a row. Easier enough mistake to make but probably shouldn't have passed peer review. Documentation is an invalable tool but only if it is correct. You can take this as a friendly reminder to make sure you include documentation reviews in your peer review process.

For reference this the stl version v4.05:0009 what ships with Microsoft Visual Studio 2005

No comments: