Opened 14 years ago

Closed 14 years ago

#2532 closed Bugs (fixed)

ptr_set<T>::erase(const T&) does not update the tree

Reported by: ayba@… Owned by: Thorsten Ottosen
Milestone: Boost 1.38.0 Component: ptr_container
Version: Boost Development Trunk Severity: Problem
Keywords: ptr_set erase Cc:

Description

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

Change History (1)

comment:1 by Thorsten Ottosen, 14 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.