How to correctly call initialization and cleanup functions?

F

ferdinand.stefanus

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!
 
D

dc

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

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

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?=

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top