Now we use newlisp to write a tcp client for testing.Program:
Chenshu @ chenshu-Beijing :~ $ Newlispnewlisp v.10.4.5 64-bit on Linux IPv4/6 UTF-8 libffi, execute 'newlisp-H' for more info.> (set 'socket (net-Connect "localhost" 8888) 3> (net-send socket "")
The aboveCodeExplanation:
1. (net-connect...) is used to connect to the local port 8888. The returned socket file is assigned to the socket variable.
2. (net-send can send a string "a" to the connection represented by socket"
In this case, the service program is printed as follows;
Chenshu @ chenshu-Beijing :~ /Netbeansprojects/cppapplication_4/Dist/debugging/GNU-Linux-x86 $./cppapplication_4 count1: 1count2: 2count3: 2The new connection object is starting now .~ Connectionoperation canceledcount3: 2
Note that, after the connection analysis function is called, its member function afterreadchar will be called. Where is the error?
Because before
Async_read (socket, buffer (read_buffer _), boost: BIND (& connection: afterreadchar, this, _ 1 ));
This pointer is bound. Therefore, even if the reference count of shared_ptr is 0, The lookup function is called, and the this pointer is saved by bind_t, the ASIO framework can still call the member function of the deleted object.
This behavior is terrible, because the program may crash next time. The key issue is whether to bind shared_ptr so that as long as everything is under shared_ptr management, Asio will not call the destroyed object incorrectly.
So the next article will solve this problem.