473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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!

Jan 12 '06 #1
3 1681
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..

Jan 12 '06 #2
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

Jan 12 '06 #3
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

Jan 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Mike Qin | last post by:
Hi there, Now I'm writing code mainly in C++. And some existing c code will call some functions which are in a C++ class. I read the basic techniques introduced from a SUN's web site. Basically...
1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
9
by: Mark Stijnman | last post by:
I need to use an API in our current project and I have been writing some wrapper classes for the functionality we need. Unfortunately, before you can use any API calls, the API must be initialized,...
0
by: Kevin Altis | last post by:
OSCON 2006: Opening Innovation http://conferences.oreillynet.com/os2006/ Save the date for the 8th annual O'Reilly Open Source Convention, happening July 24-28, 2006 at the Oregon Convention...
12
by: leaf | last post by:
Hi, How to call function at runtime, based on a struct that contains the information for the function call: struct func_to_call { int function_id; // function id to call unsigned int nparams;...
69
by: MQ | last post by:
Hi all I am just wondering how most people implement cleanup in C functions. In particular, if the function opens a number of resources, these need to be released properly should an error occur...
0
by: =?Utf-8?B?RmFyc2Fk?= | last post by:
Hi I have a mfc program and compile this to dll. I wish to call main class from vb.net to run this dll. I create 3 functaion (createCRelCtrlApp,DestroyCRelCtrlApp,InitInstanceCRelCtrlApp) and...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.