资源
C++
extern "C"
{
Service* create_service( int port )
{
Settings settings;
settings.set_port( port );
auto service = new Service( settings );
std::cout << "create_service returning pointer address: " << service << std::endl;
return service;
}
void release_service( Service* service )
{
std::cout << "release_service consuming pointer address: " << service << std::endl;
delete service;
}
}
Python
from ctypes import *
library = cdll.LoadLibrary('distribution/library/libhelpers.dylib')
class Service(object):
def __init__(self, port):
self.obj = library.create_service(port)
print "__init__ address: ", self.obj
def __del__(self):
print "__del__", self.obj
library.release_service(self.obj);
安慰
create_service 返回指针地址:0x7fc3a0e330e0
初始化地址:-1595723552
删除地址:-1595723552
release_service消费指针地址:0xffffffffa0e330e0
分段错误:11
错误
异常类型:EXC_BAD_ACCESS (SIGSEGV)
异常代码:0xffffffff914d37a0 处的 KERN_INVALID_ADDRESS
构建(cmake)
设置(CMAKE_CXX_COMPILER clang++)
设置(CMAKE_CXX_FLAGS“-stdlib=libc++ -std=c++11 -Wall -Wextra -Weffc++ -pedantic”)
add_library( 助手共享 ${MANIFEST} )
target_link_libraries(助手休息床)
描述
当将 C++ 类实例作为指针返回时。Python 收到正确的地址。但是,在以后使用此地址时,它似乎已被修改。