Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 12th, 2006, 08:45 AM
ferdinand.stefanus@gmail.com
Guest
 
Posts: n/a
Default How to correctly call initialization and cleanup functions?

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!

  #2  
Old January 12th, 2006, 10:05 AM
dc
Guest
 
Posts: n/a
Default Re: How to correctly call initialization and cleanup functions?

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..

  #3  
Old January 12th, 2006, 02:35 PM
Stephan Brönnimann
Guest
 
Posts: n/a
Default Re: How to correctly call initialization and cleanup functions?

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

  #4  
Old January 12th, 2006, 02:35 PM
Stephan Brönnimann
Guest
 
Posts: n/a
Default Re: How to correctly call initialization and cleanup functions?

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles