首页 \ 问答 \ 为什么shared_ptr没有虚拟析构函数?(Why doesn't shared_ptr have a virtual descructor? (and how can I get around this?))

为什么shared_ptr没有虚拟析构函数?(Why doesn't shared_ptr have a virtual descructor? (and how can I get around this?))

我想制作一个特殊版本的shared_ptr ,它可以在创建或销毁时执行特定的操作,但我的计划似乎因为认识到shared_ptr的析构函数是非虚拟的而被挫败,这意味着当我覆盖它时,我的指针永远不会得到当它们的最后一个实例被销毁时清理干净。

想到的唯一选择是将此行为构建到我想要与我的假设自定义shared_ptr一起使用的每个类中,这是不可行的(或者在某些情况下可能)。

编辑:

我想要这个的原因是因为我想在lua中使用一些类作为userdata对象,并且我希望我使用这种方式的每个对象都有一个独特的fenv表,当所有引用都被清理时对象已被删除。 我打算使用指针的地址,因为它们键入一个包含fenv表的表。

让我们说我有一个小部件可以有其他小部件作为孩子。 我在Lua中创建了两个小部件,然后将一个小部件设置为另一个小部件,并删除对子小部件的所有lua引用(事实上它是一个用C ++处理的子节点)。 GC现在可以随时运行并移除孩子。 我不一定希望孩子运行它的析构函数,所以我想让它成为一个shared_ptr。 这样,在Lua清理它之后,C ++对象仍然可以使用它。 如果我已将值或函数分配给它的fenv,我仍然希望能够访问它们。 只有在删除对子窗口小部件的最终引用时,才能完全删除fenv tabled。


I wanted to make a special version of shared_ptr that would perform specific operations when it was created or destroyed, but my plans appear to be foiled by the realization that shared_ptr's destructor is non virtual, meaning when I override it, my pointers never get cleaned up when the last instance of them are destroyed.

The only alternative that comes to mind is to build in this behavior into every class that I want to use with my hypothetical custom shared_ptr, and that's not feasible (or possible in some cases).

Edit:

The reason I want this is because I want to use some classes as userdata objects in lua, and I want each one of my objects that I use this way to have a fenv table unique to it that will be cleaned up when all references to the object have been removed. I plan on using the address of the pointer as they key into a table that holds the fenv table.

Lets say I have a widget that can have other widgets as children. I create two widgets in Lua, then set one as the child of the other and remove all lua references to the child widget (the fact that it's a child is handled in C++). The GC can now run at any time and remove the child. I don't necessarily want the child to have it's destructor run though, so I want to make it a shared_ptr. That way, C++ objects can still use it after Lua has cleaned it up. If I've assigned values or functions to it's fenv I still want to be able to access them. Only when the final reference to my child widget is removed do I want the fenv tabled to be removed totally.


原文:https://stackoverflow.com/questions/3801648
更新时间:2023-01-15 06:01

最满意答案

您可能没有在python 3安装上安装ipykernel 。 要安装python内核,正确的步骤是:

pip3 install ipykernel

python3 -m ipykernel install --user

看到这里


I was able to resolve the issue by reinstalling python3 using anaconda which comes with Jupyter included in its package.

You can install anaconda by follwoing the commands in here: https://conda.io/docs/user-guide/install/linux.html

相关问答

更多
  • Jupyter内核应该在$ JUPYTER_DATA_DIR中。 在OSX上,这是〜/ Library / Jupyter。 请参阅: http : //jupyter.readthedocs.org/en/latest/system.html Jupyter kernels should go in $JUPYTER_DATA_DIR. On OSX, this is ~/Library/Jupyter. See: http://jupyter.readthedocs.org/en/latest/syst ...
  • 你正在倒退; Sage在其中包含Jupyter笔记本,您可以用这种方式在内核中使用它。 (以及其他)。使用 $ sage --notebook ipython 我相信。 另见这里 。 (我认为还有一些人通过编辑某些文件或配置来重定向他们的Jupyter来“看”Sage内核,在Arch Linux上这显然是支持的......) You are going about it backwards; Sage includes the Jupyter notebook inside of it, and you ...
  • 我昨天回答了类似的问题 ,但是对于Debian来说。 以下是Digital Ocean公司新推出的Fedora 23机器。 你需要在一个版本的Python上安装python-pip , python-pip3 ,安装jupyter ,然后为另一个版本的Python安装内核。 首先,确保安装了这些依赖项: dnf groupinstall 'Development Tools' dnf install gcc-c++ dnf install rpm-build dnf install python-devel ...
  • 要为不同版本的Python安装Jupyter内核,请运行: conda create -n py36 'python=3.6' ipykernel # Replace `3.6` with desired version 要在所有conda环境中启用新安装的Jupyter内核,请运行: source activate py36 python -m ipykernel install --user 现在,当您从根环境启动Jupyter时,您可以选择在单击“内核 - >更改内核”时选择其他Python内核 ...
  • 在此提供可读性和易于搜索的答案。 @ thomas-k是对的。 conda使它变得轻而易举。 这里的完整文档应该指导如何管理python3和python2 。 https://conda.io/docs/user-guide/tasks/manage-environments.html Providing the answer here for readability and easy search. @thomas-k was right. conda makes it a breeze. The ful ...
  • 您的错误不是由于选择了内核。 你的命令%run 只能运行python,但它必须是脚本,而不是笔记本。 您可以查看ipython magic命令的详细信息 对于你的用例,我建议在jupyter中安装python和R内核。 然后你可以使用魔术单元命令%%R来选择为python笔记本内的单元运行R内核。 资料来源: 这篇关于jupyter的文章 - 提示19 其他解决方案是将R代码放入R脚本中,然后从jupyter笔记本中执行。 为此,您可以从将执行脚本的jupyter笔记本运行bash命令 !R path/to ...
  • 您可能没有在python 3安装上安装ipykernel 。 要安装python内核,正确的步骤是: pip3 install ipykernel python3 -m ipykernel install --user 看到这里 I was able to resolve the issue by reinstalling python3 using anaconda which comes with Jupyter included in its package. You can install anac ...
  • 我曾经有过同样的问题。 我最终用pip3安装了jupyter以获取python3内核。 OK, thanks everyone for answering but my issue was a little different. When creating my environment I did not add the anaconda option So instead of $ conda create -n py35 python=3.5 anaconda I used $ conda creat ...
  • 使用安装脚本创建一个zip文件。 将zip文件放在对象存储中。 使用http或curl从对象存储下载zip到bluemix笔记本环境。 请为zip文件安装pip。 Create a zip file with setup script. Put the zip file in object storage. Download zip using http or curl from object storage into bluemix notebook environment. Do pip install ...
  • 你在使用pip3 install jupyter吗? 一个更强大的解决方案是用pipenv之类的东西隔离你的环境。 pip install pipenv pipenv install --three 这将使用python3安装虚拟环境 安装包 pipenv install jupter 激活环境 pipenv shell 启动笔记本 jupyter notebook python3 Are you using pip3 install jupyter? a more robust solution ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)