Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to correctly call initialization and cleanup functions?

Reply
Thread Tools

How to correctly call initialization and cleanup functions?

 
 
ferdinand.stefanus@gmail.com
Guest
Posts: n/a
 
      01-12-2006
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!

 
Reply With Quote
 
 
 
 
dc
Guest
Posts: n/a
 
      01-12-2006
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..

 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-12-2006
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

 
Reply With Quote
 
=?iso-8859-1?q?Stephan_Br=F6nnimann?=
Guest
Posts: n/a
 
      01-12-2006
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

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
value & default initialization, and copy initialization Taras_96 C++ 3 10-30-2009 10:51 AM
copy initialization and direct initialization from C++ Primer pauldepstein@att.net C++ 5 03-26-2009 06:32 PM
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
API initialization and cleanup Mark Stijnman C++ 9 11-24-2005 04:01 PM



Advertisments