Search code examples
c++builder-xe2

Global variable initialization & finalization


I have a critical section that is shared between two threads:

TCriticalSection        lock_measDataBuff;

I have declared this variable as global. Now because Delphi style classes must be constructed using operator new, i have modified above declaration as follows:

TCriticalSection        *lock_measDataBuff;

Where is the best place to initialize the lock variable using operator new? Where is the best place to finalize the global variable using operator delete? Should it be WinMain method? Constructor of one of the classes accessing lock variable? Or some other place in the code?


Solution

  • I would use std::auto_ptr or boost::unique_ptr to handle all of that for you, eg:

    #include <memory>
    
    std::auto_ptr<TCriticalSection> lock_measDataBuff(new TCriticalSection);