#### Archived

This topic is now archived and is closed to further replies.

# Construction or linking order modifiable?

This topic is 5929 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there any way (in VC++ 6) to specify that a certain global object should be constructed before and destructed after all others? Or, as a substitute, is there any way to specify that a certain module should be linked in first (construction/destruction order of globals seems to depend on the order in which the modules are linked)? I''m writing a leak detection system that can be initialized in a global object''s constructor and shutdown in its destructor so that it''s active for the entire execution of the program without having to insert function calls. If there''s no way to make sure this object is constructed before all others then I can just not track any allocations that are made before it''s constructed, but it would be better to guarantee tracking of everything.

##### Share on other sites
That''s one of the great woes of developpers. You may try writing a makefile (yes, for VC6, I''m not joking) which specify your build order... it may cause the statics to be initialised in that same order at run-time. Modules should be linked in in the order in which they appear on the command line (or reverse order, maybe).

There is no real way of ensuring construction order though. Build order is unreliable at best, and may break the logic of your program if it ever changes... which is bad.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

##### Share on other sites
You could have a function that returns the global object (a function-level static instance). e.g.
GlobalObject * GetGlobalObject(){   static GlobalObject object;   return &object;}

This way the object will be initialized the first time the GetGlobalObject function is called, and it will always return the same instance, so all the other objects that need a GlobalObject instance just calls this function, and they are guaranteed to get a fully constructed object (assuming the constructor doesn''t throw or something like that).

Destruction may cause problems though, since ''object'' may be destructed before other classes that tries to use it. I''m not sure how it would work.

##### Share on other sites
You should never depend on the build order. If you need such a tracking system, you need the nifty counter singleton pattern. See the game engineering section for the article "The One"

##### Share on other sites
Launch your memory tracker (first) as a separate process, then spawn the application to be tracked.

1. 1
2. 2
3. 3
Rutin
19
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632628
• Total Posts
3007515
• ### Who's Online (See full list)

There are no registered users currently online

×