Subject: [Boost-bugs] [Boost C++ Libraries] #2532: ptr_set<T>::erase(const T&) does not update the tree
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2008-11-24 23:58:25
#2532: ptr_set<T>::erase(const T&) does not update the tree
-------------------------------------+--------------------------------------
Reporter: ayba_at_[hidden] | Owner: nesotto
Type: Bugs | Status: new
Milestone: Boost 1.38.0 | Component: ptr_container
Version: Boost Development Trunk | Severity: Problem
Keywords: ptr_set erase |
-------------------------------------+--------------------------------------
Hi
In some cases ptr_set<T>::erase(const T&) deletes the object as expected
but does not remove the node in the tree.
=> the tree is left with a dangling pointer
Consider the following example:
{{{
#include <boost/ptr_container/ptr_set.hpp>
#include <boost/foreach.hpp>
#include <iostream>
using namespace std;
class Int
{
public:
Int (int v) : val (v) {
cout << "\t\tnew: " << this << " -> " << val << endl;
}
~Int () {
cout << "\t\tdelete: " << this << " -> " << val << endl;
}
bool operator< (const Int& b) const { return val < b.val; }
int val;
};
int main()
{
boost::ptr_set<Int> s;
s.insert (new Int (3));
s.insert (new Int (1));
s.insert (new Int (2));
BOOST_FOREACH (Int& i, s) {
cout << &i << " -> " << i.val << endl;
}
cout << endl;
s.erase (Int(2));
BOOST_FOREACH (Int& i, s) {
cout << &i << " -> " << i.val << endl;
}
return 0;
}
}}}
--> output
{{{
new: 0x9759008 -> 3
new: 0x9759030 -> 1
new: 0x9759058 -> 2
0x9759030 -> 1
0x9759058 -> 2
0x9759008 -> 3
new: 0xbfe387ac -> 2
delete: 0x9759058 -> 2 <---- object deleted
delete: 0xbfe387ac -> 2
0x9759030 -> 1
0x9759058 -> 0 <---- dangling node/pointer
!!!
0x9759008 -> 3
delete: 0x9759030 -> 1
delete: 0x9759058 -> 0 <---- object deleted again !!!
delete: 0x9759008 -> 3
}}}
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/2532> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:59 UTC