内存核心转储 C++
Memory Core Dump C++
现在,我已经对C++调用了python。使用 ctype 在它们之间进行连接。我在 运行 时间遇到核心转储问题。
我有一个名为 "libfst.so" 的图书馆
这是我的代码。
NGramFST.h
#include <iostream>
class NGramFST{
private:
static NGramFST* m_Instace;
public:
NGramFST(){
}
static NGramFST* getInstance() {
if (m_Instace == NULL){
m_Instace = new NGramFST();
}
return m_Instace;
}
double getProbabilityOfWord(std::string word, std::string context) {
std::cout << "reloading..." << std::endl;
return 1;
}
};
NGramFST.cpp
#include "NGramFST.h"
NGramFST* NGramFST::m_Instace = NULL;
extern "C" {
double FST_getProbability(std::string word, std::string context){
return NGramFST::getInstance()->getProbabilityOfWord(word, context);
}
}
这是我的 python 代码。
from ctypes import cdll
lib = cdll.LoadLibrary('./libfst.so')
#-------------------------main code------------------------
class FST(object):
def __init__(self):
print 'Initializing'
def getProbabilityOfWord(self, word, context):
lib.FST_getProbability(word, context)
fst = FST()
print fst.getProbabilityOfWord(c_wchar_p('jack london'), c_wchar_p('my name is'))
这是错误
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
我重新检查了一遍,但我无法检测到我的问题在哪里。
ctypes
不理解C++类型(不叫c++types
)。它无法处理 std::string
。它不会知道你的函数需要 std::string
个参数。
为了使用 ctypes
,您的库需要一个 C 兼容接口。 extern "C"
是必要的,但还不够。这些函数实际上需要从 C 中调用。
更好的是,使用现代 C++/Python 绑定库,例如 pybind11。
当我更改下面的 python 代码时它起作用了
string1 = "my string 1"
string2 = "my string 2"
# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')
print fst.getProbabilityOfWord(b_string1, b_string2)
和 c++ 代码改变了下面参数的类型
FST_getProbability(const char* word, const char* context)
现在,我已经对C++调用了python。使用 ctype 在它们之间进行连接。我在 运行 时间遇到核心转储问题。
我有一个名为 "libfst.so" 的图书馆 这是我的代码。 NGramFST.h
#include <iostream>
class NGramFST{
private:
static NGramFST* m_Instace;
public:
NGramFST(){
}
static NGramFST* getInstance() {
if (m_Instace == NULL){
m_Instace = new NGramFST();
}
return m_Instace;
}
double getProbabilityOfWord(std::string word, std::string context) {
std::cout << "reloading..." << std::endl;
return 1;
}
};
NGramFST.cpp
#include "NGramFST.h"
NGramFST* NGramFST::m_Instace = NULL;
extern "C" {
double FST_getProbability(std::string word, std::string context){
return NGramFST::getInstance()->getProbabilityOfWord(word, context);
}
}
这是我的 python 代码。
from ctypes import cdll
lib = cdll.LoadLibrary('./libfst.so')
#-------------------------main code------------------------
class FST(object):
def __init__(self):
print 'Initializing'
def getProbabilityOfWord(self, word, context):
lib.FST_getProbability(word, context)
fst = FST()
print fst.getProbabilityOfWord(c_wchar_p('jack london'), c_wchar_p('my name is'))
这是错误
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
我重新检查了一遍,但我无法检测到我的问题在哪里。
ctypes
不理解C++类型(不叫c++types
)。它无法处理 std::string
。它不会知道你的函数需要 std::string
个参数。
为了使用 ctypes
,您的库需要一个 C 兼容接口。 extern "C"
是必要的,但还不够。这些函数实际上需要从 C 中调用。
更好的是,使用现代 C++/Python 绑定库,例如 pybind11。
当我更改下面的 python 代码时它起作用了
string1 = "my string 1"
string2 = "my string 2"
# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')
print fst.getProbabilityOfWord(b_string1, b_string2)
和 c++ 代码改变了下面参数的类型
FST_getProbability(const char* word, const char* context)