c++stlpurify

Purify revealed a potential free memory read when using std::list::remove()


Purify revealed a potential free memory read when using std::list::remove(). I noticed that std::list::remove() uses the type's operator== to do the comparison. However, what I have also noticed is that if the first element in the list is passed to std::list::remove(), it is deleted when it matches, but then it is still used to compare against all the other items in the list. This causes Purify to flag this as a "potential free memory read". I replaced the std::list::remove() call with erase() and an iterator, which is more efficient because it makes it loop only once versus twice in my situation.

Is there a reason std::list::remove() keeps the first element around?


Solution

  • This was GCC bug# 17012, fixed in 4.3.0. See also Library Working Group Defect Report 526.