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.