How to correctly call initialization and cleanup functions?

Discussion in 'C++' started by ferdinand.stefanus@gmail.com, Jan 12, 2006.

  1. Guest

    Hi
    I am writing a C++ class that performs video decoding using a hardware
    library. In the library documentation, it is stated that the library
    has a function that must be called once before all other functions are
    called (let's call it LIBRARY_Init()), and one function that must be
    called before the program terminates (e.g. LIBRARY_Cleanup()). I am
    thinking of putting something like the following on top of my class'
    implementation file:

    struct LibraryInitializer
    {
    LibraryInitializer()
    {
    LIBRARY_Init();
    }
    ~LibraryInitializer()
    {
    LIBRARY_Cleanup();
    }
    };

    static const LibraryInitializer init;

    //actual class implementation follows...

    Is this the correct way to do this? I have a nagging feeling that
    there's a better way to do this, but I cannot figure out how.

    Thanks!
     
    , Jan 12, 2006
    #1
    1. Advertising

  2. dc Guest

    It seems that u want to access the hardware lib. only via
    LibraryInitializer.
    In that case , use singleton pattern . I am assuming one single
    instance of LibraryInitializer
    will do..
     
    dc, Jan 12, 2006
    #2
    1. Advertising

  3. There nothing wrong with your solution, you even want to put the
    definition and instance of LibraryInitializer in the anonymos namepace:

    namespace {
    struct LibraryInitializer {
    // omiited
    };
    const LibraryInitializer init;
    }

    2 issues to consider:
    + what if LIBRARY_Init takes parameters, e.g., multi-threaded or not?
    + you should clearly document that your class takes care of
    the library initialization, else a user might call LIBRARY_Init()
    in (or before) his main().

    Regards, Stephan
     
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Jan 12, 2006
    #3
  4. here nothing wrong with your solution, you even want to put the
    definition and instance of LibraryInitializer in the anonymous
    namepace:

    namespace {
    struct LibraryInitializer {
    // omiited
    };
    const LibraryInitializer init;
    }

    2 issues to consider:
    + what if LIBRARY_Init takes parameters, e.g., multi-threaded or not?
    + you should clearly document that your class takes care of
    the library initialization, else a user might call LIBRARY_Init()
    in (or before) his main().

    Regards, Stephan
     
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Jan 12, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Stijnman

    API initialization and cleanup

    Mark Stijnman, Nov 24, 2005, in forum: C++
    Replies:
    9
    Views:
    416
  2. Kenneth Porter
    Replies:
    5
    Views:
    364
    Grizlyk
    Jan 16, 2007
  3. Jess
    Replies:
    23
    Views:
    971
  4. Replies:
    5
    Views:
    2,065
    Arne Mertz
    Mar 26, 2009
  5. Taras_96
    Replies:
    3
    Views:
    610
    Michael Tsang
    Oct 30, 2009
Loading...

Share This Page