Pointers as Map Key In Static Map Member

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 3
Reputation: Akhilleus is an unknown quantity at this point 
Solved Threads: 0
Akhilleus Akhilleus is offline Offline
Newbie Poster

Pointers as Map Key In Static Map Member

 
0
  #1
Mar 9th, 2009
I'm having a hideous problem with a program I'm working on involving a map with pointer keys. The keys are pointers (to objects on the stack) and the data are pointers (to objects on the heap). It's the list of references for a handle class, so that if handle.destroy() is called (which should delete the base (heap) object the handle "points" to), all handles to the same object will be informed of this so as not to access the data any longer. The way the program works requires objects to be destroyed in certain instances OTHER than the destruction of all handles--IE, I'm implementing pointer safety and garbage collection without affecting the normal potential of an object's memory to be deallocated.

I've isolated the source of the problem to a specific line and written the shortest possible program that reproduces it. It compiles fine, but gives an access violation (segmentation fault) when run. Also, I've tested it just using a map<int*, int*> and adding elements in the same way, which gives no errors--as it should, since the standard mandates std::less produces a meaningful ordering for pointers of the same type. But I get the problem when I do it with classes this way.

  1. #include<map>
  2. #include<set>
  3. using namespace std;
  4.  
  5.  
  6. class Handle;
  7. class Base
  8. {
  9. public:
  10. friend class Handle;
  11. private:
  12. Base() {}
  13. };
  14.  
  15. class Handle
  16. {
  17. public:
  18. static Handle TEST_OBJECT;
  19.  
  20. Handle() : ptr(new Base)
  21. {
  22. //PROBLEM IS WITH THIS FOLLOWING LINE:
  23. references[ptr].insert(this);
  24. //gives same problem on pair insertion line:
  25. //if(references.count(ptr))
  26. // references[ptr].insert(this);
  27. //else
  28. //{
  29. // set<Handle*> temp;
  30. // temp.insert(this);
  31. // references.insert(make_pair(ptr, temp));
  32. //}
  33. }
  34. private:
  35. static map<Base*, set<Handle*> > references;
  36. Base* ptr;
  37. };
  38. Handle Handle::TEST_OBJECT;
  39. map<Base*, set<Handle*> > Handle::references;
  40.  
  41. int main() { return(0); }

Does anybody know what the issue is and how it can be resolved? Thanks!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: Akhilleus is an unknown quantity at this point 
Solved Threads: 0
Akhilleus Akhilleus is offline Offline
Newbie Poster

Re: Pointers as Map Key In Static Map Member

 
0
  #2
Mar 9th, 2009
Ahhhhh! The TEST_OBJECT's constructor was being called before the map's. That was all. *Shame...*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC