View as source file or XML.
int main(int argc, char* argv[]) { void* lStore = zorba::StoreManager::getStore(); Zorba* lZorba = Zorba::getInstance(lStore); StaticContext_t lContext = lZorba->createStaticContext(); lContext->addNamespace("foo", "http://www.foo.com"); XQuery_t lQuery = lZorba->compileQuery("<foo:e/>", lContext); lQuery->execute(); lZorba->shutdown(); zorba::StoreManager::shutdownStore(lStore); return 0; }So, for this scenario, the ideal way to solve the problem is through scopes, where you implement certain part of code in methods and release them when the objects loose their reference. Alternatively, you can explicitly set the variable to null, in which case the object will automatically be released.Solution example:
int main(int argc, char* argv[]) { void* lStore = zorba::StoreManager::getStore(); Zorba* lZorba = Zorba::getInstance(lStore); { // Use a scope StaticContext_t lContext = lZorba->createStaticContext(); lContext->addNamespace("foo", "http://www.foo.com"); XQuery_t lQuery = lZorba->compileQuery("<foo:e/>", lContext); lQuery->execute(); lContext = NULL; // or explicitely set free the resources lQuery = NULL; } lZorba->shutdown(); zorba::StoreManager::shutdownStore(lStore); return 0; }
public static void main ( String argv[] ) { InMemoryStore store = InMemoryStore.getInstance(); Zorba zorba = Zorba.getInstance ( store ); StaticContext context = zorba.createStaticContext(); context.addNamespace("foo", "http://www.foo.com"); XQuery query = zorba.compileQuery("<foo:e/>", context); String result = query.execute(); zorba.shutdown(); InMemoryStore.shutdown ( store ); }In this example, and for the rest of the languages because Zorba is created in C++, we have created in the Zorba API the method destroy() that will set free the object that could be pointing to any resource from the store, this method is in every object that need to be released.Java Note: Because Java is a garbage collected language you cannot predict when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor. There is an inherited method called finalize, but this is called entirely at the discretion of the garbage collector. So, destroy() is the best practice for any language including Java.Solution example:
public static void main ( String argv[] ) { InMemoryStore store = InMemoryStore.getInstance(); Zorba zorba = Zorba.getInstance ( store ); StaticContext context = zorba.createStaticContext(); context.addNamespace("foo", "http://www.foo.com"); XQuery query = zorba.compileQuery("<foo:e/>", context); String result = query.execute(); query.destroy(); // Release memory for this XQuery context.destroy(); // Release memory for this StaticContext zorba.shutdown(); InMemoryStore.shutdown ( store ); }
XQDataSource xqdatasource = new XQDataSource(); XQConnection xqconnection = xqdatasource.getConnection(); XQStaticContext staticContext = xqconnection.getStaticContext(); staticContext.declareNamespace("foo", "http://www.foo.com"); xqconnection.setStaticContext(staticContext); XQExpression xqexpression = xqconnection.createExpression(); XQSequence xqsequence = xqexpression.executeQuery("<foo:e/>"); // code to show the output xqconnection.close(); // Closing connection frees all related resources