boost :: asio :: io_service检查是否为null
boost::asio::io_service check if null
我正在使用boost 1.55(io_service doc)。我需要在我的io_service上调用destructor,以在循环串行设备上循环以获取新数据后将其重置。问题是,当攻击器被称为两次(重试连接)时,我会得到一个分段错误。
在标题文件中
boost::asio::io_service io_service_port_1;
关闭连接的函数
io_service_port_1.stop();
io_service_port_1.reset();
io_service_port_1.~io_service(); // how to check for NULL?
// do I need to re-construct it?
以下内容不起作用:
if (io_service_port_1)
if (io_service_port_1 == NULL)
谢谢。
如果您需要对对象创建和破坏对象的手动控制,则应将其包裹在std::unique_ptr
对象中。
std::unique_ptr<boost::asio::io_service> service_ptr =
std::make_unique<boost::asio::io_service>();
/*Do stuff until connection needs to be reset*/
service_ptr->stop();
//I don't know your specific use case, but the call to io_service's member function reset is probably unnecessary.
//service_ptr->reset();
service_ptr.reset();//"reset" is a member function of unique_ptr, will delete object.
/*For your later check*/
if(service_ptr) //returns true if a valid object exists in the pointer
if(!service_ptr) //returns true if no object is being pointed to.
一般来说,您绝对不应直接致电~object_name();
。曾经。曾经。曾经。有几个原因:
- 作为堆栈放松的正常部分,当方法返回时,这将被调用。
-
delete
指针将其称为。 - "智能指针"(例如
std::unique_ptr
和std::shared_ptr
)会在自我毁灭时称其为称。
直接调用~object_name();
只能在极少数情况下进行,通常涉及分配,即使那样,通常也有更干净的解决方案。
相关文章:
- 检查字符串是否"null" C++
- 是否有通用方法可以找到任何以 null 结尾的字符串的长度?
- API 返回智能指针的 std::optional 以明确指定指针可能为 null 是否有意义?
- 如果我在相应 char 数组的声明中为其提供额外的元素,是否会自动设置 NULL?
- &*NULL 在C++中是否定义良好?
- 是否有一种安全的方法可以断言字符串视图是否以 null 终止?
- C++,检查当我无法使用 NULL/0 时是否初始化了变量
- 以 Null 结尾的字符串的"NULL"或" "符号是否存储在文件中?
- 如何检测 ofstream 是否正在写入 /dev/null
- 小写 null 在 C++ 中是否有效
- 一旦 std::weak_ptr 上的锁返回了 nullptr,它是否还会再次成为非 null
- C++如何将结构数组初始化为null,然后在while循环中检查该数组的元素是否为null
- 对 NULL 值的按位操作是否会使C++中的代码崩溃
- 移动语义是否只是一个浅层副本并将其他人的指针设置为 null?
- 测试指针是否为null的最有效方法是什么?
- boost :: asio :: io_service检查是否为null
- 我如何检查该点动态分配的单指针是否为null
- 检查指针是否为null,然后在同一if语句中取消引用它,这样安全吗
- cin函数是否添加了在输入结束时终止的null
- 在 C++11 中是否有等效的 boost::interprocess::null_mutex(例如 std::null