This behaviour is by design, although this consequence was perhaps
unintended. Given that a query is based on a connection, and ResUse is
based on a query, the order of destruction must be in the reverse of
construction.
In MySQL terms, it should make sense as well, given that a ResUse only
retrieves data as needed, and so must have a connection involved.
One way to fix your issue is to wrap your data access in a scope of its
own. You can do this with a class or a function.
Taking your example, you can modify it like this:
int main(int argc,char **argv)
{
Connection *con=new Connection(false);
con->connect(DB_DB,DB_HOST,DB_USER,DB_PASS);
{
// create a query and get results
Query q=con->query();
ResUse use=q.use("show variables like 'version'");
// show results
while(Row row=use.fetch_row()) {
cout << row.at(0) << "=" << row.at(1) << endl;
}
}
// close connection
con->close();
delete con;
}
In your application, you can create a proper scope by putting that nested
scope in a function or class of its own.
Alternatively, use C++ mechanisms for this, which is safer anyway when
exceptions are involved:
int main(int argc,char **argv)
{
std::auto_ptr<Connection> con(new Connection(false));
con->connect(DB_DB,DB_HOST,DB_USER,DB_PASS);
// create a query and get results
Query q=con->query();
ResUse use=q.use("show variables like 'version'");
// show results
while(Row row=use.fetch_row()) {
cout << row.at(0) << "=" << row.at(1) << endl;
}
}
- Chris