construction of extern objects vs. "normal ones"
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: construction of extern objects vs. "normal ones"

  1. #1
    Join Date
    Mar 2009
    Posts
    18

    Question construction of extern objects vs. "normal ones"

    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
    Code:
    ...
    EventQueue mainEventQueue("Main Event Queue");
    ...
    printout from my main (all the other EventQueues are created dynamically)
    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
    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.

    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.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: construction of extern objects vs. "normal ones"

    Hmm. You're relying on the set being initialized prior to the global EventQueue. I don't think C++ offers ordering guarantees on initialization of global objects.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,431

    Re: construction of extern objects vs. "normal ones"

    Quote Originally Posted by ssouffri View Post
    I'm seeing some strange behavior I can't explain.
    Code:
    extern EventQueue mainEventQueue;
    //...
    class EventQueue : public Serializable
    {
      //...
    static EventQueueSet eventQueueSet;
    //...
    };
    Which one of the variables in red gets initialized first? The answer is that there is no answer -- the order of globals and static initialization is undefined, unless all of your variables are defined in one source unit (then the order is the order of declaration).

    Since the "extern" variable is defined in another source unit, you're in big trouble. The static may not have been initialized yet, but you're trying to do a push_back() onto it, causing all sorts of problems.

    Regards,

    Paul McKenzie

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center