question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SEGFAULT when using typed memoryview in structs

See original GitHub issue

Here is the reproduction code:

from cpython cimport array
import array


cdef struct MyStruct:
    #float data
    float [:] data


cdef test():
    print("TEST")

    #cdef float data = 1.
    cdef array.array data = array.array('f', [1., 2., 3.])

    cdef MyStruct s = MyStruct(data)

    print(s.data[0])
    print(s)

    cdef list l = [s]
    cdef MyStruct extracted_s = l[0]

    print(extracted_s.data[0])

    # Make sure that `data` survives at least until now
    print(data)
    print("DONE")

test()

Running the code I get printed only "TEST" and s.data[0] value. print(s) and all subsequent print calls crash Python with SEGFAULT (terminated by signal SIGSEGV (Address boundary error)).

Changing float [:] to a single float (uncomment the commented lines in the example above) makes the code work just fine.

I have tried several versions of Cython (0.24.1, 0.23.5, 0.22.1) with Python 3.5.2 and Python 2.7.12 on Arch Linux x64, and Python 2.7.3 on Ubuntu 12.04. The result is always the same - SEGFAULT.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcCotecommented, Feb 22, 2018

I agree with @c-f-h. We also use a similar trick to handle memviews inside struct. Us too found ourselves in a similar situation and would have to significantly rewrite part of our code.

Could we raise a warning instead of an error (at least for 0.28)?

0reactions
scodercommented, Feb 23, 2018

I disallowed this because supporting structs with memoryviews specifically as cdef class members would require us to then disallow any assignments from this member to variables (i.e. copy these structs by value), as well as taking the address of the struct, which is more difficult to track. It’s much easier to disallow the whole thing. Also, while I understand that you have already structured your code using this “feature”, I can’t see a reason why supporting this is better than requiring users to make the memoryview a class member rather than a sub-struct member. It just leads to different ways of accessing the view, but IMHO makes it clearer what the responsibilities for the lifetime management are.

Sorry for the inconvenience, but this “feature” was inherently unsafe before. No offence, @MarcCote and @c-f-h , but I wouldn’t be surprised if your code bases were also suffering from memory leaks and/or crashes under certain (rare) conditions, similar to what the OP reported. Even if you took good care to avoid this, it’s an easy source of bugs for new developers in your teams who are less familiar with the constraints.

@c-f-h, I would consider a pull request that implements this as a new feature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Struct definition with typed memoryview as member
I get a Segmentation fault. Is there anything obvious I am overlooking or do typed memoryviews not work in structs?
Read more >
Issue 7433: MemoryView memory_getbuf causes segfaults ...
I think this is an implementation issue in MemoryView rather than an issue with the buffer interface. PEP 3118 states, "This same bufferinfo ......
Read more >
Contiguous memory and finding segfaults in Cython | /bin/ħ
Cython tries to push us towards using typed memoryview objects that also have a Python-compatible view and can be constructed from any ...
Read more >
Troubleshooting and tips — Numba 0.50.1 documentation
When Numba tries to compile your code it first tries to work out the types of all the variables in use, this is...
Read more >
Segmentation Fault when Initializing a Structure
Hi all, I am trying to import a text data from a .dat file in C code to run tests on an algorithm....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found