- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
首先,这个问题似乎与同时使用 clang(任何版本)和高于 6.5.0 版本的 libstdc++ 有关。
我在我的代码库中使用以下习惯用法来对用户隐藏实现:
#include <memory>
class myclass : public std::enable_shared_from_this<myclass> {
class impl;
protected:
myclass() = default;
public:
myclass(myclass&&) = delete;
myclass(myclass const&) = delete;
myclass& operator=(myclass&&) = delete;
myclass& operator=(myclass const&) = delete;
virtual ~myclass() = default;
static std::shared_ptr<myclass> create();
int get();
};
class myclass::impl : public myclass {
public:
using myclass::myclass;
int get_impl() {
return 33;
}
};
std::shared_ptr<myclass> myclass::create() {
return std::make_shared<impl>();
}
int myclass::get() {
return static_cast<impl*>(this)->get_impl();
}
int main() {
auto ref = myclass::create();
return ref->shared_from_this()->get();
}
这个成语使用了一个继承并实现公共(public)基类的私有(private)类。在 ubuntu 18.04 下使用 clang++ -O3 -std=c++11 main.cpp && ./a.out
运行此代码段时,代码段崩溃并显示以下输出:
terminate called after throwing an instance of 'std::bad_weak_ptr'
what(): bad_weak_ptr
使用以下回溯:
#0 0x00007ffa76a7de97 in raise () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007ffa76a7f801 in abort () from /lib/x86_64-linux-gnu/libc.so.6
#2 0x00007ffa774728fb in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3 0x00007ffa77478d3a in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007ffa77478d95 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5 0x00007ffa77478fe8 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6 0x0000000000404f7c in std::__throw_bad_weak_ptr() ()
#7 0x0000000000404e92 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::__shared_count(std::__weak_count<(__gnu_cxx::_Lock_policy)2> const&) ()
#8 0x0000000000404e2f in std::__shared_ptr<myclass, (__gnu_cxx::_Lock_policy)2>::__shared_ptr<myclass, void>(std::__weak_ptr<myclass, (__gnu_cxx::_Lock_policy)2> const&) ()
#9 0x0000000000404df8 in std::shared_ptr<myclass>::shared_ptr<myclass, void>(std::weak_ptr<myclass> const&) ()
#10 0x0000000000403d2c in std::enable_shared_from_this<myclass>::shared_from_this() ()
#11 0x0000000000403ac8 in main ()
测试平台运行以下编译器和标准库:
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/7.3.0
尽管此代码在其他平台和/或编译器上运行良好:
总的来说,在高于版本 6.5 的 libstdc++ 中,从父类继承时,std::make_shared
似乎没有检测到从 std::shared_from_this
的继承.0 使用任何 clang 版本时。
是否可以在保持习惯用法的同时解决这个问题?
什么可能导致这里的缺陷?这是否应该报告给任何错误跟踪器(但哪个是正确的,因为这似乎是 clang 和 libstdc++ 之间的互操作性问题)。
最佳答案
正如您已经建议的那样,问题确实似乎是 std::shared_ptr
没有检测到 std::enable_shared_from_this
基类。一种适用于我的机器的解决方法:
std::shared_ptr<myclass> myclass::create() {
return std::shared_ptr<myclass>{static_cast<myclass*>(new impl{})};
}
至于在哪里报告错误:我会在 llvm bugtracker 上报告它,毕竟它可以与 GCC 一起使用,并且与 GCC 库兼容符合 clangs 的利益。
关于c++ - 从基类继承 std::shared_from_this 时的 std::bad_weak_ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53901474/
我正在学习智能指针和shared_from_this。在Class Inheritance Relations中,会很难理解。 我有两个基类CA和CB,它们派生自enable_shared_from_
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: enable_shared_from_this - empty internal weak pointer?
我想创建一些 Timer 类,它每 N 秒打印一次“文本”,其中 N 将在构造函数中初始化。 #include #include #include #include class Timer :
我使用 boost asio 和 beast(用于浏览器支持)开发桌面聊天。 我使用这个架构: 但是,在构建时,我遇到了一个问题:bad_weak_ptr,我不知道哪里出了问题:s这是源代码的链接 h
我试图将一个共享指针从 QTGUI 类传递到 Client 类,但一直收到 bad weak ptr 错误。我读到我不能直接在构造函数中分配 shared_from_this(),因为此时指针还没有准
当我这样做时出现异常:std::bad_weak_ptr->shared_from_this() template class painter_record_t { ....... private:
我正在尝试在 asio 中保留已连接客户端的列表。我已经从文档 (http://www.boost.org/doc/libs/1_57_0/doc/html/boost_asio/example/cp
我想了解为什么在调用 shared_from_this 时会出现 bad_weak_ptr 异常。 #include #include class parent : public std::ena
我开发了一些可以正确编译但在(调试)运行时失败的代码。我正在使用 VS2015。 背景:我正在构建一个高级消息引擎。为了使新消息的编程添加可维护,在生产代码中,我花时间使用 explicit init
我有一个 SuperParent 类,一个 Parent 类(派生自 SuperParent)并且都包含一个 shared_ptr到一个 Child 类(它包含一个 weak_ptr 到一个 Supe
为了创建我的 EventManager,我需要创建一些函数,这些函数会使用 Listeners 的 shared_ptr 将它们存储到 vector 中并调用它们的事件函数。我这样做了,它工作正常,除
我正在将 BOOST 移植到我的 TI 嵌入式平台,在嵌入式环境中不支持异常是很常见的,所以我的 TI 嵌入式平台支持。所以我使用 BOOST_EXCEPTION_DISABLE 和 BOOST_NO
我有一个基类和一个派生类。 API 仅公开 Derived,而实现隐藏在 Base 中(gcc 属性 visibility 设置为 hidden ),因为一些内部 API 模块必须调用 Base 方法
编辑:我从来没有弄明白这一点——我重构了代码,使其与 Boost 示例几乎相同,但问题仍然存在。如果其他人遇到此问题,则您的问题可能是更常见的 shared_from_this() 在不存在 shar
我定义了一个类似于下面的类: class A : std::enable_shared_from_this { public: static std::shared_ptr create() {
我试图理解 std::enable_shared_from_this 类的行为,但我无法理解。所以我写了一个简单的程序来测试不同的情况。 问题 谁能解释一下下面代码的行为,因为我无法解释观察到的结果。
我正在使用独立的 Asio 和 C++11 创建一个 C++ 服务器应用程序,但遇到错误,这就是我寻求帮助的原因。 错误 在类里面worker_thread , 在通话期间 shared_from_t
首先,我已阅读列出的所有相关问题。 他们说,“在使用 shared_from_this 之前,你必须有一个指向 this 的现有 shared_ptr。”据我所知,我绝不会违反该条件。我将 Foo 的
首先,这个问题似乎与同时使用 clang(任何版本)和高于 6.5.0 版本的 libstdc++ 有关。 我在我的代码库中使用以下习惯用法来对用户隐藏实现: #include class mycl
这个问题在这里已经有了答案: std::enable_shared_from_this: is it allowed to call shared_from_this() in destructor
我是一名优秀的程序员,十分优秀!