I have a template class with a static function (see Connect function below). This template class is part of a 3rd Party lib (call it LibA). My code uses this lib and includes the header below since I need to use the template class. When I compile my lib without inline functions (-fno-default-inline with gcc) I get no problems. When I compile in release (-O2), my application crashes.
I was wondering what the implications are of having a static function in a template and how inlining may affect this.
template<class T>
class TCPConnector
: public IOHandler {
private:
string _ip;
uint16_t _port;
vector<uint32_t> _protocolChain;
bool _closeSocket;
Variant _customParameters;
public:
TCPConnector(int32_t fd, string ip, uint16_t port,
vector<uint32_t>& protocolChain, const Variant& customParameters)
: IOHandler(fd, IOHT_TCP_CONNECTOR) {
_ip = ip;
_port = port;
_protocolChain = protocolChain;
_closeSocket = true;
_customParameters = customParameters;
}
virtual ~TCPConnector() {
//FINEST("Close socket: %d", _closeSocket);
if (_closeSocket) {
close(_fd);
//FINEST("Socket closed!");
}
}
static bool Connect(string ip, uint16_t port,
vector<uint32_t>& protocolChain, Variant& customParameters) {
protoent *pProtocol = getprotobyname("IP");
if (pProtocol == NULL) {
FATAL("Unable to resolve protocol number for IP");
return 0;
}
int32_t fd = (int32_t) socket(PF_INET, SOCK_STREAM, pProtocol->p_proto);
if (fd <= 0) {
FATAL("Unable to create fd");
return 0;
}
if (!SetFdNonBlock(fd)) {
FATAL("Unable to put socket in non-blocking mode");
return false;
}
TCPConnector<T> *pTCPConnector = new TCPConnector(fd, ip, port,
protocolChain, customParameters);
if (!pTCPConnector->Connect()) {
IOHandlerManager::EnqueueForDelete(pTCPConnector);
FATAL("Unable to connect");
return false;
}
return true;
}
};