织梦CMS - 轻松建站从此开始!

罗索

boost::asio Buffer debugging 导致程序crash的问题

罗索客 发布于 2010-01-17 19:11 点击:次 
本文作者:Chris Kohlhoff -- Author of the Boost.Asio library for networking in C++. 这几句英语看得懂吧!!所以应该绝对相信其权威性了。我的程序crash也许就是这个问题导致的,但由于时间关系还未根据他讲的那样重新编译来试。先记下来!!!
TAG:

原文:http://bbs.rosoo.net/forum.php?mod=viewthread&tid=81

本文作者:Chris Kohlhoff
Author of the Boost.Asio library for networking in C++.
这几句英语看得懂吧!!所以应该绝对相信其权威性了。我的程序crash也许就是这个问题导致的,但由于时间关系还未根据他讲的那样重新编译来试。先记下来!!!
还有Mr Kohlhoff的Blog地址:http://blog.think-async.com/2006/11/buffer-debugging.html

Buffer debugging
Some standard library implementations, such as the one that ships with MSVC 8.0, provide a nifty feature called iterator debugging. What this means is that the validity of your iterators is checked at runtime. If you try to use an iterator that has been invalidated, you'll get an assertion. For example:

std::vector<int> v(1)std::vector<int>::iterator i = v.begin();
v.clear(); // invalidates iterators
*i = 0; // assertion!

Boost.Asio now takes advantage of this feature to add buffer debugging. Consider the following code:

void dont_do_this(){
 std::string msg = "Hello, world!";
 asio::async_write(sock, asio::buffer(msg), my_handler);
}

When you call an asynchronous read or write you need to ensure that the buffers for the operation are valid until the completion handler is called. In the above example, the buffer is the std::string variable msg. This variable is on the stack, and so it goes out of scope before the asynchronous operation completes. If you're lucky, your application will crash. Often you will get random failures.

With the new buffer debug checking, however, Boost.Asio stores an iterator into the string until the asynchronous operation completes, and then dereferences it to check its validity. In the above example you get an assertion failure just before Boost.Asio tries to call the completion handler.

This feature has only been tested with MSVC 8.0 so far, but it should work with any other implementation that supports iterator debugging. Obviously there's a performance cost to this checking, so it's only enabled in debug builds. You can also explicitly disable it by defining BOOST_ASIO_DISABLE_BUFFER_DEBUGGING (or ASIO_DISABLE_BUFFER_DEBUGGING if you're using standalone asio).

(Chris Kohlhoff)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201001/8320.html]
本文出处:http://bbs.rosoo.net 作者:Chris Kohlhoff
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容