我使用mex C ++接口将C ++代码包装到Matlab中。尽管编译成功,但是在代码中运行函数时Matlab崩溃了,我不知道是什么原因造成的!我正在使用Matlab 2019a,并且正在使用以下代码
#include "mex.hpp"
#include "mexAdapter.hpp"
#include "RBergomi.h"
using namespace matlab::mex;
using namespace matlab::data;
class MexFunction : public matlab::mex::Function {
public:
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
Vector H(1);
H= Vector { 0.07};
Vector eta(1);
eta= Vector { 1.9};
Vector rho(1) ;
rho= Vector { -0.9};
Vector T(1) ;
T = Vector { 1.0};
Vector K(1) ;
K= Vector { 1.0 };
double xi = 0.0552;
int N = 2;
long M = 1;
Vector W1(2);
W1= Vector { 1.0, 0.0};
Vector W1perp(2);
W1perp = Vector { 1.0 ,0.0};
RBergomiST Obj=RBergomiST(xi, H, eta, rho, T, K, N, M);
double RB;
price(RB, Obj, W1, W1perp );
outputs[0][0] = RB;
}
void price(double RB, RBergomiST Obj, const Vector& W1, const Vector& W1perp) {
RB=Obj.ComputePayoffRT_single(W1, W1perp);
}
};
我做了编译
mex -g Bergomi_interface.cpp -lfftw3 RBergomi.cpp ParamTot.cpp BlackScholes.cpp
运行成功,但是运行Matlab时我崩溃了,并且出现了此消息。
我试图检查Valgrind是否存在内存泄漏,并且在修复了一些内存泄漏后仍然得到了最终输出,但是Matlab仍然崩溃了。
== 6751 == Memcheck,内存错误检测器
== 6751 ==朱利安·塞沃德(Julian Seward)等人版权所有(C)2002-2015和GNU GPL。
== 6751 ==使用Valgrind-3.11.0和LibVEX;使用-h重新运行以获取版权信息
== 6751 ==命令:./ RBergomi
== 6751 ==
== 6751 ==
== 6751 ==堆摘要:
== 6751 ==退出时使用:1块中的72,704字节
== 6751 ==堆总使用量:1,455个分配,1,454个空闲,368,936个字节分配
== 6751 ==
== 6751 == 1的丢失记录1中仍然可以访问72,704个字节的块
== 6751 ==在0x4C2DB8F:malloc(在/ usr / lib / valgrind / vgpreload_memcheck-
amd64-> linux.so)
== 6751 ==通过0x52C1EFF:??? (在/ usr / lib / x86_64-linux-
gnu / libstdc ++。so.6.0.21)
== 6751 ==通过0x40106C9:call_init.part.0(dl-init.c:72)
== 6751 ==通过0x40107DA:call_init(dl-init.c:30)
== 6751 ==通过0x40107DA:_dl_init(dl-init.c:120)
== 6751 ==通过0x4000C69:??? (在/lib/x86_64-linux-gnu/ld-2.23.so中)
== 6751 ==
== 6751 ==泄漏摘要:
== 6751 ==绝对丢失:0字节,位于0块中
== 6751 ==间接丢失:0个字节,共0个块
== 6751 ==可能丢失:0字节,共0块
== 6751 ==仍可访问:1块72,704字节
== 6751 ==已抑制:0字节,分为0块
== 6751 ==
== 6751 ==对于检测到和抑制的错误的计数,请使用-v
重新运行== 6751 ==错误摘要:0个上下文中存在0个错误(禁止:0个上下文中有0个)