A telnet server may have
The smart pointer helps here, because we can clear it, which both deletes the object and changes the pointer to a null pointer. We now have a dangling pointer to the deleted telnet object.
#A TELNET SERVER MAY HAVE CODE#
It's probably better to allow our function to throw exceptions, rather than returning a status code that the caller could forget to examine. It's generally a bad idea to catch all kinds of exception like this. If we really want it to continue in the background we should detach() it first. It's a bad idea to destruct a running joinable thread. boost::thread t(boost::bind(&boost::asio::io_service::run, &io_service)) īoost:thread is not declared - I recommend including to get a definition in scope. A smart pointer takes care of that for us. We have a memory leak here, as we have not freed the previous value of telnet before assigning this new one. We can simply pass our existing strings: tcp::resolver::query query(host, port) Is there a need to copy the string from host? We're going to pass this to the resolver::query constructor, which accepts a const std::string&, so it's completely pointless to copy the characters to create a new string. bool Ephemeris::Connect(std::string host, std::string port)ĭo we really need modifiable copies of host and port? It looks like we should be fine with const references: bool Ephemeris::Connect(const std::string& host, const std::string& port) We can improve on this by making telnet a smart pointer (probably std::unique_ptr), so that a default destructor is sufficient.Ĭhanging to smart pointer also means we don't need to override or delete the copy constructor and assignment operator, which we have failed to do (meaning that after copying, one object's destructor would delete the other's data). The test here is pointless, since delete works fine with a null argument. No need for a constructor if it does nothing. The original code is this one, " Boost Telnet Client". If you have any suggestions, related or not with my doubt, please, tell me. Run the IO service as a separate thread, so the main thread can block on standard inputīoost::thread t(boost::bind(&boost::asio::io_service::run, &io_service)) Telnet = new TelnetClient(io_service, iterator) Tcp::resolver::iterator iterator = resolver.resolve(query) Tcp::resolver::query query(chost, cport) Resolve the host name and port number to an iterator that can be used to Strcpy_s(cport, port_length, port.c_str()) Convert std::string port to char* port. Strcpy_s(chost, host_length, host.c_str())
Convert std::string host to char* host.
I think they are in the right place, but I have doubts because in C++ doesn't exist finally.īool Connect(std::string host, std::string port) īool Ephemeris::Connect(std::string host, std::string port) I have this class and I don't know where to put the following two lines because of the catch: delete chost