Jump to content
  • Advertisement

Archived

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

Qoy

Construction or linking order modifiable?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!