I'm seeing some strange behavior I can't explain. I have the following code:
in eventq.hh
Code:class EventQueue; // forward declaration extern EventQueue mainEventQueue; ... class EventQueue : public Serializable { private: std::string objName; Event *head; void insert(Event *event); void remove(Event *event); public: typedef std::vector<EventQueue *> EventQueueSet; static EventQueueSet eventQueueSet; EventQueue(const std::string &n) : objName(n), head(NULL) { eventQueueSet.push_back(this); std::cout << "EventQueue(" << n << ") = " << this << "\n"; } ... };
in eventq.cc
printout from my main (all the other EventQueues are created dynamically)Code:... EventQueue mainEventQueue("Main Event Queue"); ...
Now I would expect the code in the constructor to get executed. However when I test this I only see a printout "EventQueue(Main Event Queue) = 0xde3f90" but mainEventQueue doesn't seem to be added to eventQueueSet. But since I see the printout I would think the constructor got executed. So it appears as if I the eventQueueSet got overwritten with a new EventQueueSet after the mainEventQueue was constructed.Code:EventQueue(Main Event Queue) = 0xde3f90 ... EventQueue(system_EventQueue) = 0x1341660 EventQueue(instruction-based event queue) = 0x138dff0 EventQueue(load-based event queue) = 0x138e070 ... iterating over eventQueueSet: ######## (EventQueue=0x1341660) != (mainEventQueue=0xde3f90), eventQueueSet.size()=3 ######## (EventQueue=0x138dff0) != (mainEventQueue=0xde3f90), eventQueueSet.size()=3 ######## (EventQueue=0x138e070) != (mainEventQueue=0xde3f90), eventQueueSet.size()=3
How do get the mainEventQueue pointer in the eventQueueSet.
Side note:
I also tried "typedef std::set<EventQueue *> EventQueueSet;" instead of std::vector but then I got a segmentation fault. Both issues probably have the same underlying problem.




Reply With Quote
