I have a small code snippet that uses Thrift for network communications.
int main() {
while (true) {
boost::shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
CalculatorClient client(protocol);
try {
transport->open();
client.ping();
cout << "ping()" << endl;
// following line is commented out intentionally
//transport->close();
} catch (TException& tx) {
cout << "ERROR: " << tx.what() << endl;
}
}
}
My question is: Does boost::shared_ptr close connection once destroyed? If yes, then transport->close();
can be commented out without any problems, right?
Looking at the source, I don't see TTransport doing anything in its destructor. However, the destructor of TSocket (src), does call it's close() function.
Since the shared_ptr was created in the scope of your main function and no one else has asked for a pointer to the object in care of the shared_ptr, 'socket' will destruct after it goes out of scope.
TBufferedTransport does not appear to explictly declare a destructor, however, it does own a TSocket, which will got out of scope when TBufferedTransport destructs, thus the TSocket's destructor gets called.
TBufferedTransport::[rBuf_/wBuf_] are scoped_arrays so I don't think you need to worry about those either.