为什么这种新的使用会导致分割错误?

2024-09-30 02:30:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我以以下方式使用placement new,那么我的代码似乎运行可靠。如果我使用注释代码,我经常得到一个分段错误(11)或意外的结果。返回静态分配对象的地址也有效。你知道吗

我已经验证了不会返回null,并且对于测试脚本中的单个Python对象,这个函数只被调用一次。你知道吗

extern "C"
{    
    void * hail_detector_new()
    {
        alignas(alignof(hail::Detector)) static U8 allocation[sizeof(hail::Detector)];

        // return new(std::nothrow) hail::Detector;

        return new(allocation) hail::Detector;
    }
}

Python参数和返回类型声明:

f = _library.hail_detector_new

f.restype = c_void_p

f.argtypes = None

class Detector(object):

    def __init__(self):

        self._object =  _library.hail_detector_new()

版本

Squall: clang --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Squall: python --version
Python 2.7.13 :: Continuum Analytics, Inc.

Tags: 对象代码selfnewreturnobjectversionlibrary

热门问题