dca_interface  6.3.4
Using global variables

The use of global variables is not recommended.

A preferred solution would be to create variables in your main() (or similar) function and to pass them via reference to other functions as required.

void main()
{
myDca = dca::DcaInstance::create( myInitData );
...
// put all necessary instances to subsequent functions via reference...
DoWork( myDca, myLicense, myUrlDbClassifier );
}

This ensures that once the DoWork() function returns, the DcaInstance goes out of scope, and all variables are destructed in the reverse creation order.

The object instances can be placed in a separate function as well as the deinit code.

For more information on this please refer to Startup/Shutdown sequence.

If you're using many more variables than in our example, you may also use a so called context:

class MyDcaContext
{
public:
MyDcaContext() : inShutDown (false)
{ }
virtual ~MyDcaContext()
{
// signal DCA worker threads to shutdown
inShutDown = true;
// this cancels the download or update task if it is currently running
updateModule.cancelUpdate();
// this cancels all long-lasting functions and forces them to return immediately
// such as database merge processes...
dcaInstance.signal( DCA_SIG_ABORT );
// ...
// wait for worker threads to finish
// ...
// all other DCA objects are destructed in reverse construction order
}
// initialize the DCA objects
virtual bool Init();
// Construction order is important!
// the DCA instance must always be created first.
dca::DcaInstance dcaInstance;
dca::InitData initData;
dca::License license;
dca::UpdateModule updateModule;
dca::DbConnectionData connectData;
dca::DbConnection dbConnection;
dca::UrlClassification urlClassification;
dca::UrlDbClassifier urlDbClassifier;
volatile bool inShutDown;
...
};
void main ()
{
MyDcaContext myContext;
myContext.dcaInstance = dca::DcaInstance::create( myInitData );
...
// put all necessary instances to subsequent functions via reference...
DoWork( myContext );
}

So it's very easy to add more global-like variables to your context and create them in the scope of your main() function.

Since the assignment operators of all classes have been overloaded, you will not need to deal with checks for visibility, where to destruct variables or even where to addref or release an object.

This is all done automatically by the wrapper layer of the SCA API.

The update module is used to download and install DCA content and engine updates.
Definition: base_classes.h:917
const unsigned int DCA_SIG_ABORT
Currently the only supported signal parameter for the DcaInstance::signal() function.
Stores the connection data for a database.
Definition: base_classes.h:815
Database connection class for a local or remote database.
Definition: base_classes.h:859
Main class for the URL classification.
URL database classifier class.
Use a License to initialize a classification package or a toolbox package.
Definition: base_classes.h:560
Encapsulates the init and deinit of the DCA API.
Definition: base_classes.h:315
This structure is used to initialize the DcaInstance.
Definition: base_classes.h:264
static DcaInstance create(const InitData &initData)
Creates a DcaInstance, starts up the DCA API and initializes the required main module.
int main(int argc, char *argv[])
The main routine.