Boost: ASIO connection management 3

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.