This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Template data deallocation bug?


Hi folks,

Attached is a file that exhibits what I think is a bug in deallocation 
of template-based data. I've distilled this from my larger system. The 
basic idea of the "token" class is that it does reference counting on 
the values. The strange thing is that the reference count during 
deallocation goes 3 -> 2 -> 0 instead of 3 -> 2 -> 1 as expected. If I 
make set_of_sets local to main(), it works as expected...

Perhaps I'm missing something?

Thanks,
David
#include <map>
#include <set>
#include <iostream>

using namespace std;

class token
{
private:
  token(){};
public:
  token(int in_id)
  {
    cout << "In constructor\n";
    id = in_id;

    Increase_Reference_Count();
  }

  token(const token& in_token)
  {
    cout << "In copy constructor\n";
    id = in_token.id;

    Increase_Reference_Count();
  }

  virtual ~token()
  {
    cout << "In destructor\n";
    Decrease_Reference_Count();
  }

  friend bool operator== (const token& in_first, const token& in_second) { return in_first.id == in_second.id; }
  friend bool operator< (const token& in_first, const token& in_second) { return in_first.id < in_second.id; }

protected:
  void Decrease_Reference_Count() const
  {
    cout << "Before ref count decrease id " << id << " has " << references[id] << " references" << endl;

    assert(references[id]>0);
    references[id]--;

    cout << "Decreased ref count for id " << id << " to " << references[id] << endl;
  }

  void Increase_Reference_Count() const
  {
    if (references.find(id) == references.end())
      references[id] = 1;
    else
      references[id]++;

    cout << "Increased ref count for id " << id << " to " << references[id] << endl;
  }

  int id;

  static map<int, int> references;
};

// -------------------------------------------------------------------------------------------------

set<set<token> > set_of_sets;

map<int, int> token::references;

int main()
{
  set<token> tokens1;

  token e1(3);
  tokens1.insert(e1);
  set_of_sets.insert(tokens1);

  cout << "exiting and deallocating\n";

  return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]