- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我们有一个模板类,它拥有一些 std::unique_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_;
};
它有two constructors我们制作了一个包装函数,据称可以简化事情:
template <class cloud_type,
bool keep_alive = true,
class socket_type = asio_http,
class error_handle = default_error_handler,
class ...args,
typename =
typename std::enable_if<!std::is_same<cloud_type,
cloud_batch>::value, bool>>
callable<cloud_type,keep_alive,socket_type,error_handle>
call(typename cloud_type::callback functor,
args... params)
{
return callable<cloud_type,
keep_alive,
socket_type,
error_handle>(functor, default_node, params...);
}
我们中的两个人已经在带有 G++ 5.9 和 Boost 1.58 的 Ubuntu 16.04 上对此进行了测试。当我们构建一个 Release
版本(使用 -O3
)时,应用程序工作正常。但是,当我们构建一个 Debug
版本时,应用程序会出现 SEGFAULT。我得到的实际错误是:
Thread 1 "human_detection" received signal SIGSEGV, Segmentation fault.
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
305 { return std::get<0>(_M_t); }
这似乎源于:
#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
指向:
void callable<cloud_type,
keep_alive,
socket_type,
error_handle
>::send(unsigned int timeout)
{
assert(socket_ && query_ && resol_ && io_);
if (!socket_)
throw std::runtime_error("socket not set");
if (!io_ || !query_ || !resol_)
throw std::runtime_error("io, query or resolver not set");
object.fill_buffer(boost::ref(*buffer_.get()), endpoint);
socket_->is_connected() ?
socket_->send(*query_.get(), *resol_.get(), timeout, *buffer_.get()) :
socket_->begin(*query_.get(), *resol_.get(), timeout);
io_->run();
io_->reset(); // here !!!
}
我试图了解我们做错了什么,我猜测 Release
使用 RVO,而 Debug
制作了一个拷贝,导致上述结果?
当我调用包装器时:
auto query = call<human_detection,false>(
[&](std::vector<noos::object::human> humans) {
std::cout << "Found " << humans.size() << " humans!" << std::endl;
}, pic);
错误在 Debug
期间仍然存在,而如果我直接调用类构造函数,SEGFAULT 就会消失:
callable<human_detection,false> query([&](std::vector<noos::object::human> humans) {
std::cout << "Found " << humans.size() << " humans!" << std::endl;
}, default_node, pic);
我担心的是类的资源(例如,boost::asio::io
)存在潜在问题,这是通过创建类来实现的可复制。
我解决了 SEGFAULT,这是由于通过引用捕获的 lambda,一个正在发布的 (this)。但是,我原来的问题仍然存在;为什么在 DEBUG 期间发生这种情况而不是在 RELEASE 期间发生?
最佳答案
复制 unique_ptr
当然有一个基本问题,这是不可能的。这就是独特的意义所在。编译器通过禁用其复制构造函数来强制执行此操作。因此 callable
的默认复制构造函数也不存在。
但它确实看起来您的callable
可以被复制。我怀疑发生的事情是你不小心写了一个转换构造函数:
callable::callable(callback functor, platform = default_node);
我怀疑您的 callable
可以转换为 callback
并最终通过此路由被复制。
根据经验,可使用一个参数调用的构造函数(可能在添加默认值之后)应该是显式
。
顺便说一句:您的其他 ctor 也有逻辑错误:
template <typename... parameters>
callable(callback functor,
platform info = default_node,
parameters... args);
究竟如何使用这个默认值?它只能在提供单个参数的情况下使用,但随后会启动重载决策并选择第一个 ctor。也就是说,它在当前形式下也是一个潜在的单参数构造函数,也应该是显式
。
关于c++ - 调试版本 SEGFAULTs 而发布版本有效 - 是 RVO 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46911813/
所以我需要一个简单的分配器来分配(有时使用清零)并随后从映射内存池中释放 4K block 。然而,在实现这个之后,在测试时我发现在释放一两个 block 之后,如果我尝试分配一个 block ,程序
我的任务是用 C 编写一个程序。该程序应该能够检查参数并创建与我提供的参数一样大的数组。我必须用随机数填充数组。到目前为止工作正常。稍后我的任务是使用指针对数组进行排序。第一件事是我不太明白指针是如何
我对 C 很陌生(仍然)所以如果我误解了一些基本的东西,请耐心等待 我有一个简单的程序,它应该将文件作为字符串读取,然后将该字符串拆分成行 - 将结果存储到 n 个字符串数组中。但是,当我运行以下代码
我在工作中使用的应用程序之一遇到了一个奇怪且烦人的问题。该应用程序是用 C++ 编写的,当应用程序终止(主函数返回或调用 exit)时,它会因段错误而崩溃。段错误似乎是由 basic_string 类
我使用 python swig 包装的 C++ 库。在它的 __init__.py 文件中,它 sets在导入包含实现代码的共享对象文件之前,使用 dlopen 标志 RTLD_GLOBAL。 这会导
我在这里遇到了段错误。我很困惑。请帮帮我。 f1 和 y 都是结构体节点的指针。我想把 y 的左转 f1 右转。 #include #include struct node{
我有一个在公共(public)结构中声明的数组,如下所示: uint16_t *registers; 在一个函数中,我正在检索一个字符字符串(存储在缓冲区中,请参阅下面的代码),其中包含以逗号分隔的数
我正在用 C 实现二叉搜索树。下面的代码工作正常,只是当我尝试从树中删除子树时得到 SEGFAULT: 源代码: #include #include struct node { int dat
struct vehicle *add_vehicle(struct vehicle *v){ struct vehicle *newcar = (struct vehicle*)malloc
我正在使用链接列表实现符号表,代码工作正常,但代码中存在内存泄漏, 我有以下结构 struct node { char* pcKey; void* pvValue; struct node
我正在尝试将字符串复制到数组并打印它。它适用于第一个 for 循环,但第二次出现 seg 错误。 main (int argc, char *argv[]){ int argcIndex; cha
自从我用 C 编写代码已经一年了,但我不明白为什么会出现段错误 // Assume all imports are made int printAgain(double** array, int si
这是我的代码。编辑:调用者包含在底部。 该函数读取数据文件,确定有多少行和列,然后将数据存储到 data_array 中。 int getdata(double* *data_array, int*
我认为有两组代码是等效的,但一组会导致段错误,而另一组则不会。我真的很困惑为什么会这样...... 我想创建一个查找函数 此代码确实有效: MyPair *> dummy(x, NULL);
希望有人能提供帮助。我可以毫无错误地编译,我没有发现任何语法错误,但是当我运行它时,它崩溃了。在启动时调试段错误。全面披露,这是作业。我不是要找人来编写这个代码,只是看看我的问题和我现有的代码,也许会
我正在尝试在OpenMP中并行化相当大的for-loop。大约有20%的时间运行正常,但其余时间会因各种段错误而崩溃,例如: *** glibc detected *** ./execute: dou
我有一个模板类 ISingleton class ISingleton { public: static T* getInstance() { lock_guard g
我正在为使用 LibSVM 的 Android 构建 NDK 应用程序。我在 XCode 中为我的 mac 构建了一个等价物(都是 C++) 我发现 Mac 可以高速准确地处理我给它的非常大的特征向量
我在 ARM linux 平台上有一个由简单代码引起的非常奇怪的崩溃。问题是它很少重现(一天一次),另一个问题是它在实际上无法重现的地方崩溃。 让我们从 C++ 代码开始。线程函数执行此操作:
我有这段代码 int main() { int *b = new int(8); cout<<" &b = "<
我是一名优秀的程序员,十分优秀!