我遇到了 SWIG 过早删除临时 C++ 对象的问题。
Python 测试脚本的示例输出:
--------------------------------------------------------------------------------
Works as expected:
b0 = Buffer(0, 0, 0, )
b1 = Buffer(1, 1, 1, )
b0 = Buffer(0, 0, 0, 1, 1, 1, )
y = Buffer(0, 0, 0, 1, 1, 1, )
b1 = Buffer(1, 1, 1, )
repr(b0) = Buffer(id = 0, vector at 0x020bf450, data at 0x020aeb30, size = 6)
repr(y) = Buffer(id = 0, vector at 0x020bf450, data at 0x020aeb30, size = 6)
Funny business:
Deleting Buffer(id = 2)
Deleting Buffer(id = 3)
repr(b2) = Buffer(id = 2, vector at 0x020bf790, data at 0x00, size = 4257068)
Deleting Buffer(id = 4)
repr(b3) = Buffer(id = 4, vector at 0x02037040, data at 0x0204a4e0, size = 6)
Deleting Buffer(id = 0)
Deleting Buffer(id = 1)
Deleting Buffer(id = X)正在从内部生成 Buffer::~Buffer() C++ 代码,所以我们可以在这里看到 Funny business部分,C++ Buffer 对象被删除得太早了! Python 对象 b2和 b3应该使用 id=2 保存对 C++ Buffer 对象的引用和 id=4 .
我所有的代码都附在我的博客post关于这个问题。但是,我将在这里总结代码:
缓冲区.hpp:
#include <vector>
#include <string>
struct Buffer
{
Buffer();
Buffer(const Buffer & copy);
~Buffer();
Buffer & operator=(const Buffer & rhs);
Buffer & operator<<(const Buffer & rhs);
Buffer & operator<<(double rhs);
std::string __str__() const;
std::string __repr__() const;
private:
std::vector<double> _data;
int _id;
};
swig_test.i:
%module swig_test
%include "std_string.i"
%{
#include "Buffer.hpp"
#include <iostream>
%}
%ignore Buffer::operator=;
%include "Buffer.hpp"
go_test.py:
from swig_test import Buffer
def zeros(n):
'''
Returns a Buffer filled with 'n' zeros.
'''
b = Buffer()
for i in xrange(n):
b << 0.0
return b
def ones(n):
'''
Returns a Buffer filled with 'n' ones.
'''
b = Buffer()
for i in xrange(n):
b << 1.0
return b
def main():
#--------------------------------------------------------------------------
# This sections works as expected
print "-" * 80
print "Works as expected:"
b0 = zeros(3)
print " b0 = ", b0
b1 = ones(3)
print " b1 = ", b1
y = b0 << b1
print " b0 = ", b0
print " y = ", y
print " b1 = ", b1
print " repr(b0) = ", repr(b0)
print " repr(y) = ", repr(y)
#--------------------------------------------------------------------------
# Funny things are happening here!
print "Funny business:"
b2 = zeros(3) << ones(3)
print " repr(b2) = ", repr(b2)
b3 = zeros(3) << 4.0
print " repr(b3) = ", repr(b3)
if __name__ == "__main__":
main()
我已按照我的博文中的概述尝试使用 SWIG 进行一些操作,但我的 SWIG-foo 技能却不够用。
拯救我的 SO 社区,你是我唯一的希望!
更新 1
我怀疑我有多个 PyObject持有Buffer *到相同的 C++ Buffer对象,所以当临时PyObject被垃圾收集,它删除 C++ Buffer *连同它。
所以,我想我需要一个 Py_INCREF某处,但在哪里?
更新 2
尝试按照 jarod42 的建议按值返回打破了串联范式,例如:
b = Buffer()
b << 1 << 2 << 3
print b
只产生:
Buffer(1, )
所以这不是我想要的。
%newobject指令可用于释放函数或方法创建的新创建对象(防止内存泄漏)。在这种情况下,Buffer::operator<<不是在创建新对象。
最佳答案
经过更多搜索,我发现了这个 thread这最终会导致一个可行的解决方案。
将 typemap(out) 与 Py_INCREF 结合使用可以达到目的。
swig_test.i:
%module swig_test
%include "std_string.i"
%{
#include "Buffer.hpp"
#include <iostream>
%}
%ignore Buffer::operator=;
%typemap(out) Buffer & operator<<
{
if(result) { /* suppress unused warning */ }
Py_INCREF($self);
$result = $self;
}
%include "Buffer.hpp"
现在我得到了我想要的行为(与正常运行的纯 Python 实现相匹配)并且没有内存泄漏。
关于python - SWIG、C++ 和 Python : C++ temporary objects deleted too soon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27727364/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我想解析一个已经存在的.mid文件,改变它的乐器,例如从“acousticgrandpiano”到“violin”,然后将它保存回去或作为另一个.mid文件。根据我在文档中看到的内容,该乐器通过program_change或patch_change指令进行了更改,但我找不到任何在已经存在的MIDI文件中执行此操作的库.他们似乎都只支持从头开始创建的MIDI文件。 最佳答案 MIDIpackage会为您完成此操作,但具体方法取决于midi文件的原始内容。一个MIDI文件由一个或多个音轨组成,每个音轨是十六个channel中任何一个上的
本文主要介绍在使用Selenium进行自动化测试或者任务时,对于使用了iframe的页面,如何定位iframe中的元素文章目录场景描述解决方案具体代码场景描述当我们在使用Selenium进行自动化测试的时候,可能会遇到一些界面或者窗体是使用HTML的iframe标签进行承载的。对于iframe中的标签,如果直接查找是无法找到的,会抛出没有找到元素的异常。比如近在咫尺的例子就是,CSDN的登录窗体就是使用的iframe,大家可以尝试通过F12开发者模式查看到的tag_name,class_name,id或者xpath来定位中的页面元素,会抛出NoSuchElementException异常。解决
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
ValidPalindromeGivenastring,determineifitisapalindrome,consideringonlyalphanumericcharactersandignoringcases. [#125]Example:"Aman,aplan,acanal:Panama"isapalindrome."raceacar"isnotapalindrome.Haveyouconsiderthatthestringmightbeempty?Thisisagoodquestiontoaskduringaninterview.Forthepurposeofthisproblem