Why might this use of new cause a segmentation fault?

Viewed 73

If I use placement new in the following way my code appears to run reliably. If I use the commented code instead I frequently get either a segmentation fault (11) or unexpected results. Returning the address of a statically allocated object also works.

I've verified that null is not returned and that this function is being called exactly once for the single Python object in my test script.

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 arguments and return type declarations:

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()

Versions

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.
0 Answers
Related