1

メインエンジンを呼び出すために使用している共有ライブラリに関数ポインターがあります。(うまくいきます) : func_ptr

また、boost::python::import("module") を使用してプログラムにインポートする python モジュールもあります。

私のpythonモジュールの関数:

def wrapper(function):
    function('TEST ')

私のC ++プログラムの関数:

int function(char const *msg){
{
    func_ptr(msg); //this line crashes
    return 1;
}

ラッパー関数を呼び出しているとき

module.attr("wrapper")(boost::python::make_function(function))

私のC ++関数でクラッシュします。(セグメンテーション)

gdb はそのようなものを生成します:

http://pastebin.com/NRdupqp6

それを機能させる方法は?タイ!

4

1 に答える 1

0

function()関数 pointer の呼び出し時にクラッシュが発生した場合func_ptr、実行は既に Boost.Python レイヤーを通過しています。検討:

  • func_ptr有効な関数を指していることを確認しています。
  • func_ptrポイント先の関数が引数値を適切に処理することをテストします"TEST "

module.py:

def wrapper(function):
   return function('TEST ')

そしてmain.cpp

#include <iostream>
#include <boost/python.hpp>

void engine(const char* msg)
{
  std::cout << "engine: " << msg << std::endl;
}

void (*func_ptr)(const char* msg) = &engine;

int function(const char* msg)
{
  func_ptr(msg);
  return 42;
}

int main()
{
  Py_Initialize();

  namespace python = boost::python;
  try
  {
    // Import the 'module' module.
    python::object module = python::import("module");

    // Invoke the wrapper function in the module, providing function
    // as a callback.
    python::object result = 
        module.attr("wrapper")(python::make_function(&function));

    std::cout << "result: " << python::extract<int>(result) << std::endl;
  }
  catch (python::error_already_set&)
  {
    PyErr_Print();
  }
}

アプリケーションは次の出力を生成します。

engine: TEST 
result: 42
于 2013-11-15T14:10:49.153 に答える