[Boost] example of a down machine generated by using boost: function and boost: bind

Source: Internet
Author: User

ServerCodeThe function and bind dependent on boost are used for callback. This type of down Machine occurs during asynchronous calls. Let's talk less about the Code:

 
VoidFN (STD ::StringStr );Char* Temp_str =New Char[24]; Strcpy (temp_str,"1212");
Boost: function f=Boost: BIND (FN, temp_str );F ();

The f function object here stores a temp_str pointer. If it is a synchronous call, a temporary STD: string variable will be generated, and a copy of temp_str will be generated. Afterwards, temp_str will not be referenced, so there will be no problems;

However, if it is an asynchronous call, temp_str may be a wild pointer. If temp_str is released, the process of constructing a STD: string from const char * may be down. this is one of our servers (a batch of accurate descriptions) that has had bugs for more than two years.

 

Although you know the cause of the Bug, it is easy to find a solution, that is, to explicitly generate a temporary variable and let boost copy it .......

 
STD ::StringTemp_string =Temp_str; Boost: function f= Boost: BIND (FN, temp_string );

 

However, I was wondering why a variable of the function parameter signature type is not saved directly when boost was originally designed. Instead, I saved a variable after bind. after all, the exact types of function parameters have been derived from the bind. this issue does not occur if he directly saves a variable of the signature type.

 

PS:

Boost: bind, boost: function, boost Lambda are all lazy during design, that is, the value of inertia. lambda expressions including C ++ 11 may also be evaluate by inertia.
Pay attention to this when using it.

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.