Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调试版本错误,而发布版本有效-是RVO吗?_C++_C++11 - Fatal编程技术网

C++ 调试版本错误,而发布版本有效-是RVO吗?

C++ 调试版本错误,而发布版本有效-是RVO吗?,c++,c++11,C++,C++11,我们有一个模板类,它拥有一些std::unique\u ptr,其中一些是boost::asio template <class cloud_type, bool keep_alive = true, class socket_type = asio_http, class error_handle = default_error_handler> class callable { callable() =

我们有一个模板类,它拥有一些
std::unique\u ptr
,其中一些是
boost::asio

template <class cloud_type,
          bool  keep_alive   = true,
          class socket_type  = asio_http,
          class error_handle = default_error_handler>
class callable
{
    callable() = delete;
    // other stuff here, click the link to see actual code
private:
    // other members, most are unique pointers
    std::unique_ptr<boost::asio::io_service> io_;
};
这似乎源于:

#0  0x0000000000443128 in std::unique_ptr<boost::asio::io_service, std::default_delete<boost::asio::io_service> >::get (this=0x120) at /usr/include/c++/5/bits/unique_ptr.h:305
#1  0x0000000000441880 in std::unique_ptr<boost::asio::io_service, std::default_delete<boost::asio::io_service> >::operator-> (this=0x120) at /usr/include/c++/5/bits/unique_ptr.h:298
#2  0x000000000043f2b4 in noos::cloud::callable<noos::cloud::human_detection, false, noos::cloud::asio_http, noos::cloud::default_error_handler>::send (this=0x0, timeout=0) at /home/zuperath/code/noos-api-maria/./noos/cloud/callable.tpl:120
该错误在
Debug
期间持续存在,但是如果我直接调用类构造函数,SEGFAULT就会消失:

callable<human_detection,false> query([&](std::vector<noos::object::human> humans) {
                        std::cout << "Found " << humans.size() << " humans!" << std::endl;
                     }, default_node, pic);
可调用查询([&](std::vector humans){

std::cout复制
唯一的ptr
当然有一个根本性的问题,这是不可能的。这就是唯一的意义所在。编译器通过禁用其复制构造函数来实现这一点。因此,
可调用的默认复制构造函数也不存在

但是它确实看起来像你的
可调用的
可以被复制。我怀疑发生的是你意外地写了一个转换构造函数:

callable::callable(callback functor, platform = default_node);
我怀疑您的
可调用
可以转换为
回调
,并最终通过此路径复制

根据经验,可以用一个参数调用的构造函数(可能在添加默认值之后)应该是显式的

顺便说一句:您的另一个ctor也有一个逻辑错误:

template <typename... parameters>
callable(callback functor,
         platform info = default_node,
         parameters... args);
模板
可调用(回调函子,
平台信息=默认_节点,
参数…args);

具体如何使用此默认值?仅当提供单个参数时才可以使用此值,但随后将启动重载解析并选择第一个ctor。也就是说,它也是当前形式的潜在单参数ctor,并且应该是显式的

您在
可调用::se中有
this==0
nd
。添加
断言(此!=0)
callable::send
中,运行调试程序并深入检查调用堆栈。@DanielSęk断言未被触发。我认为有一个对象的延迟副本带有无效/移动的
asio::io
不知何故您破坏了
这个
指针。上面的堆栈跟踪显示
这个==0
和后者
这个==0x120
for成员。这表明
callable::send
或从中调用的方法破坏了
This
,或
callable::send
是用
This==0
调用的。堆栈跟踪太浅,但我认为
callable::send
有时在空指针上被调用。@DanielSęk是的,我也看到了,但我认为e方法被调用了两次,所以我认为问题出在别处,SEFAULT就是它的症状…谢谢你的帮助!谢谢你的回答,是的,我发现了它并删除了
default\u节点
。但是我不确定我是否理解,你是在暗示我以某种方式将
可调用的
转换为
callback
在什么地方?@Ælex:我想知道在
call
的返回语句中是否确实发生了这种情况,恐怕不是。这个SEGFAULT似乎是在我调用copy构造函数时发生的。我已经显式地删除了它,并直接构造了可调用的segmentation,而segmentation错误就消失了。返回类型是
callable
not
callback
虽然我承认我使用的措辞很危险@Ælex:我不明白你删除一个由于
unique\u ptr
成员而被删除的副本怎么会有不同。(?)不只是删除它,也不使用它。当我不删除它时,它不是由编译器创建的吗?或者隐式移动语义是否使用移动运算符?我的印象是,我有同一对象的两个副本,一个具有有效的io服务,另一个已被移动或无效。
auto query = call<human_detection,false>(
                 [&](std::vector<noos::object::human> humans) {
                    std::cout << "Found " << humans.size() << " humans!" << std::endl;
                 }, pic);
callable<human_detection,false> query([&](std::vector<noos::object::human> humans) {
                        std::cout << "Found " << humans.size() << " humans!" << std::endl;
                     }, default_node, pic);
callable::callable(callback functor, platform = default_node);
template <typename... parameters>
callable(callback functor,
         platform info = default_node,
         parameters... args);