Library Initialization and Cleanup |
The Database toolkit contains two routines that must be invoked when using any class.
Failure to initialize a toolkit produces different effects, depending on the library. In some cases, the library may work perfectly; at other times the library can behave erroneously or cause an abnormal program termination.
There are three methods for initializing a Recursion Software toolkit.
The Database toolkit uses a reference counting mechanism to track multiple initialization attempts, so it is safe to use all three techniques in the same application. Library initialization is not thread safe, and should only be performed by the primary, or main, thread.
Each toolkit provides a functional interface with the common format
os_init_library_toolkit();
os_exit_library_toolkit();
where library is the name of the component.
The following code fragment shows how the functional interface can be used with Database<ToolKit>.
#include <ospace/std/iostream>
#include <ospace/network.h>
#include <ospace/stream.h>
#include <ospace/database.h>
using OS_STD_IO cout;
using OS_STD_IO endl;
void
main()
{
os_init_network_toolkit(); // Initialize dependency
os_init_streaming_toolkit(); // Initialize dependency
os_init_database_toolkit(); // Initialize Database<ToolKit>
// Use Database<ToolKit>
try
{
os_ip_address iaddr( os_host::my_host().name() );
os_db_connection connection1( os_socket_address( iaddr, 3006) );
cout << "Found database server on local machine." << endl;
}
catch(...)
{
cout << "Error connecting to database server on local machine." << endl;
}
os_exit_database_toolkit(); // Cleanup Database<ToolKit>
os_exit_streaming_toolkit(); // Cleanup dependency
os_exit_network_toolkit(); // Cleanup dependency
}
Each toolkit contains an initialization object that provides automatic library initialization and cleanup. All initialization objects have the common name
class os_library_toolkit
where library is the name of the component.
The following code fragment shows how the initialization object can be used with Database<ToolKit>.
#include <ospace/std/iostream>
#include <ospace/network.h>
#include <ospace/stream.h>
#include <ospace/database.h>
using OS_STD_IO cout;
using OS_STD_IO endl;
void
main()
{
os_network_toolkit init_network; // Instantiate dependency
os_streaming_toolkit init_streaming; // Instantiate dependency
os_database_toolkit init_database; // Instantiate the Database<ToolKit> initialization object
// Use Database<ToolKit>
try
{
os_ip_address iaddr( os_host::my_host().name() );
os_db_connection connection1( os_socket_address( iaddr, 3006) );
cout << "Found database server on local machine." << endl;
}
catch(...)
{
cout << "Error connecting to database server on local machine." << endl;
}
}
For development on platforms that support dynamic link libraries (DLLs), automatic library initialization can occur through the DLL initialization routine. Although this technique is more convenient than the techniques discussed previously, it uses platform-specific features and can result in non-portable code. Consequently, we recommend using one of the other two initialization techniques discussed.
If your platform supports DLLs, then automatic library initialization is provided by default. Consult the online release notes for more information about your specific platform.
Copyright©2005-2026 Recursion
Software LLC
All Rights Reserved - For use by licensed users only.