为什么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 thatshared_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
最满意答案
您可能没有在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 ...
-
将.egg文件添加到Bluemix上的Jupyter IPython Notebook(Add .egg file to Jupyter IPython Notebook on Bluemix)[2023-06-07]
使用安装脚本创建一个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 ...