使用 C++在 Mac 上序列化数字的流会导致 boost.python 崩溃

发布于 2024-10-06 08:44:09 字数 3875 浏览 0 评论 0原文

我认为我的链接有问题(我在 Mac 上运行,并且必须将库更改为模块?)。这是我所拥有的:

CMakeLists.txttutorial.cpp

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

project (testme)

FIND_PACKAGE( Boost REQUIRED )
FIND_PACKAGE( Boost COMPONENTS python REQUIRED )
FIND_PACKAGE( PythonLibs REQUIRED )

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREAD ON)

include_directories(${Boost_INCLUDE_DIRS})
include_directories(${PYTHON_INCLUDE_PATH})

add_library(testme MODULE tutorial.cpp)
target_link_libraries(testme ${PYTHON_LIBRARY} ${Boost_PYTHON_LIBRARY})

运行a.py

#include <boost/python.hpp>
struct TestClass { TestClass() {} };

std::ostream &operator<<(std::ostream &ostr, const TestClass &ts)
{
    ostr << 1 << "\n";
    return ostr;
}

BOOST_PYTHON_MODULE(libtestme)
{
    using namespace boost::python;
    class_<TestClass>("TestClass", init<>())
        .def(self_ns::str(self))
        ;
}

a.py

#!/usr/bin/env python
import libtestme as d
a = d.TestClass()
print a

会崩溃,除非 ostr << 1<<; "\n"; 被注释掉或替换为 ostr << “1”<< “\n”;

构建并运行:

cmake .
make
./a.py

堆栈跟踪(执行gdb --args python a.py之后):

#0  0x00007fff817003d6 in __kill ()
#1  0x00007fff817a0972 in abort ()
#2  0x000000010069baf2 in uw_init_context_1 ()
#3  0x000000010069bf38 in _Unwind_Resume ()
#4  0x00000001004a1283 in boost::detail::lexical_cast<std::string, TestClass, true, char> (arg=@0x100343a70, buf=0x7fff5fbfe307 "", src_len=0) at lexical_cast.hpp:1153
#5  0x00000001004a0db4 in boost::lexical_cast<std::string, TestClass> (arg=@0x100343a70) at lexical_cast.hpp:1174
#6  0x00000001004a0b64 in boost::python::detail::operator_1<(boost::python::detail::operator_id)19>::apply<TestClass>::execute (x=@0x100343a70) at operators.hpp:357
#7  0x00000001004a4417 in boost::python::detail::invoke<boost::python::to_python_value<_object* const&>, _object* (*)(TestClass&), boost::python::arg_from_python<TestClass&> > (rc=@0x7fff5fbfe406, f=@0x10033e318, ac0=@0x7fff5fbfe3f0) at invoke.hpp:75
#8  0x00000001004a3fb1 in boost::python::detail::caller_arity<1u>::impl<_object* (*)(TestClass&), boost::python::default_call_policies, boost::mpl::vector2<_object*, TestClass&> >::operator() (this=0x10033e318, args_=0x100452a10) at caller.hpp:223
#9  0x00000001004a3ba7 in boost::python::objects::caller_py_function_impl<boost::python::detail::caller<_object* (*)(TestClass&), boost::python::default_call_policies, boost::mpl::vector2<_object*, TestClass&> > >::operator() (this=0x10033e310, args=0x100452a10, kw=0x0) at py_function.hpp:38
#10 0x000000010061797a in boost::python::objects::function::call ()
#11 0x0000000100617d10 in boost::detail::function::void_function_ref_invoker0<boost::python::objects::(anonymous namespace)::bind_return, void>::invoke ()
#12 0x000000010061f431 in boost::python::handle_exception_impl ()
#13 0x0000000100614b5e in function_call ()
#14 0x000000010000c4a2 in PyObject_Call ()
#15 0x000000010001e8bd in instancemethod_call ()
#16 0x000000010000c4a2 in PyObject_Call ()
#17 0x00000001000ba2f7 in PyEval_CallObjectWithKeywords ()
#18 0x0000000100077c0a in slot_tp_str ()
#19 0x000000010005aafa in _PyObject_Str ()
#20 0x000000010005b0dd in internal_print ()
#21 0x0000000100032ccd in PyFile_WriteObject ()
#22 0x00000001000bf09b in PyEval_EvalFrameEx ()
#23 0x00000001000c2936 in PyEval_EvalCodeEx ()
#24 0x00000001000c2a56 in PyEval_EvalCode ()
#25 0x00000001000e744e in PyRun_FileExFlags ()
#26 0x00000001000e7709 in PyRun_SimpleFileExFlags ()
#27 0x00000001000fde8c in Py_Main ()
#28 0x0000000100000f14 in dyld_stub_strlen ()

I think something is wrong with my linking (I am running on a mac, and had to change the library to be a MODULE?). Here's what I have:

CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

project (testme)

FIND_PACKAGE( Boost REQUIRED )
FIND_PACKAGE( Boost COMPONENTS python REQUIRED )
FIND_PACKAGE( PythonLibs REQUIRED )

set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREAD ON)

include_directories(${Boost_INCLUDE_DIRS})
include_directories(${PYTHON_INCLUDE_PATH})

add_library(testme MODULE tutorial.cpp)
target_link_libraries(testme ${PYTHON_LIBRARY} ${Boost_PYTHON_LIBRARY})

tutorial.cpp

#include <boost/python.hpp>
struct TestClass { TestClass() {} };

std::ostream &operator<<(std::ostream &ostr, const TestClass &ts)
{
    ostr << 1 << "\n";
    return ostr;
}

BOOST_PYTHON_MODULE(libtestme)
{
    using namespace boost::python;
    class_<TestClass>("TestClass", init<>())
        .def(self_ns::str(self))
        ;
}

a.py

#!/usr/bin/env python
import libtestme as d
a = d.TestClass()
print a

Running a.py crashes unless ostr << 1 << "\n"; is commented out or replaced with ostr << "1" << "\n";!

Build and run:

cmake .
make
./a.py

Stack trace (after doing gdb --args python a.py):

#0  0x00007fff817003d6 in __kill ()
#1  0x00007fff817a0972 in abort ()
#2  0x000000010069baf2 in uw_init_context_1 ()
#3  0x000000010069bf38 in _Unwind_Resume ()
#4  0x00000001004a1283 in boost::detail::lexical_cast<std::string, TestClass, true, char> (arg=@0x100343a70, buf=0x7fff5fbfe307 "", src_len=0) at lexical_cast.hpp:1153
#5  0x00000001004a0db4 in boost::lexical_cast<std::string, TestClass> (arg=@0x100343a70) at lexical_cast.hpp:1174
#6  0x00000001004a0b64 in boost::python::detail::operator_1<(boost::python::detail::operator_id)19>::apply<TestClass>::execute (x=@0x100343a70) at operators.hpp:357
#7  0x00000001004a4417 in boost::python::detail::invoke<boost::python::to_python_value<_object* const&>, _object* (*)(TestClass&), boost::python::arg_from_python<TestClass&> > (rc=@0x7fff5fbfe406, f=@0x10033e318, ac0=@0x7fff5fbfe3f0) at invoke.hpp:75
#8  0x00000001004a3fb1 in boost::python::detail::caller_arity<1u>::impl<_object* (*)(TestClass&), boost::python::default_call_policies, boost::mpl::vector2<_object*, TestClass&> >::operator() (this=0x10033e318, args_=0x100452a10) at caller.hpp:223
#9  0x00000001004a3ba7 in boost::python::objects::caller_py_function_impl<boost::python::detail::caller<_object* (*)(TestClass&), boost::python::default_call_policies, boost::mpl::vector2<_object*, TestClass&> > >::operator() (this=0x10033e310, args=0x100452a10, kw=0x0) at py_function.hpp:38
#10 0x000000010061797a in boost::python::objects::function::call ()
#11 0x0000000100617d10 in boost::detail::function::void_function_ref_invoker0<boost::python::objects::(anonymous namespace)::bind_return, void>::invoke ()
#12 0x000000010061f431 in boost::python::handle_exception_impl ()
#13 0x0000000100614b5e in function_call ()
#14 0x000000010000c4a2 in PyObject_Call ()
#15 0x000000010001e8bd in instancemethod_call ()
#16 0x000000010000c4a2 in PyObject_Call ()
#17 0x00000001000ba2f7 in PyEval_CallObjectWithKeywords ()
#18 0x0000000100077c0a in slot_tp_str ()
#19 0x000000010005aafa in _PyObject_Str ()
#20 0x000000010005b0dd in internal_print ()
#21 0x0000000100032ccd in PyFile_WriteObject ()
#22 0x00000001000bf09b in PyEval_EvalFrameEx ()
#23 0x00000001000c2936 in PyEval_EvalCodeEx ()
#24 0x00000001000c2a56 in PyEval_EvalCode ()
#25 0x00000001000e744e in PyRun_FileExFlags ()
#26 0x00000001000e7709 in PyRun_SimpleFileExFlags ()
#27 0x00000001000fde8c in Py_Main ()
#28 0x0000000100000f14 in dyld_stub_strlen ()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

超可爱的懒熊 2024-10-13 08:44:09

在上面的示例中,构造函数调用必须为:

a= d.TestClass()

否则该示例在带有 gcc 4.4.3 和 boost 1.40 的 Ubuntu 10.04LTS 上运行良好

In the example above the constructor call needs to be:

a= d.TestClass()

otherwise the example works fine on Ubuntu 10.04LTS with gcc 4.4.3 and boost 1.40

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文