Destructor being called on SGI hash_map key

This is a discussion on Destructor being called on SGI hash_map key within the C++ Programming forums, part of the General Programming Boards category; I'm using the hash_map that comes with gcc 4.0 (i.e. <ext/hash_map>) and I have no idea why the destructor is ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Destructor being called on SGI hash_map key

    I'm using the hash_map that comes with gcc 4.0 (i.e. <ext/hash_map>) and I have no idea why the destructor is being called on a key object.

    I have a class object for which I've defined a hash function and a destructor. Then when I
    Code:
    some_class sc;
    hash_map<sc, whatev, hash<sc> > hm;
    hm[sc] = yadayada;
    the destructor is called on sc. I found this because my program would crash due to some_class::~some_class() being called twice. Since ~some_class() frees a dynamic member within, calling it twice will obviously cause a crash.

    I went through the code of ext/hash_map and I couldn't find a place where a destructor would be called after hashing the key value. Does anyone have any ideas?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,147
    Code:
    some_class sc;
    hash_map<sc, whatev, hash<sc> > hm;
    hm[sc] = yadayada;
    Am I on "Candid Camera" or do you honestly expect to get any help at all posting crap like that?

    Soma

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Quote Originally Posted by phantomotap View Post
    Am I on "Candid Camera" or do you honestly expect to get any help at all posting crap like that?
    Yes, you are. Smile. (Do you honestly expect a civilized answer responding with crap like that?)

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    some_class probably doesn't have a copy constructor and/or assignment operator.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    It's never really a question of why is my copy-constructor / assignment-operator / destructor being called in cases like this. The problem is always that you haven't followed the "rule of three":
    If you must implement any of the above, then you need to implement all three. Alternatively you may disable copy-construction or assignment by defining then as private and leaving them unimplemented.

    Failure to follow the rule of three or disable copy-construction and assignment, is noted as a bug in anything I code review.

    If you want specific help on how to implement these then you'll have to post your real class definition.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. key problem
    By madsmile in forum Game Programming
    Replies: 20
    Last Post: 06-05-2002, 10:57 PM
  2. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 10:28 AM
  3. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  4. heeeeeeellllllpppppppp!!!
    By DarkDays in forum C++ Programming
    Replies: 15
    Last Post: 12-09-2001, 08:43 PM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21