Open charlieb opened 1 year ago
Is a reference counting problem. After v[2:].begin()
, the temporary produced by v[2:]
gets ref-counting to zero, deleting its memory, while the iterator returned from begin()
still points into it. Python ref-counting is a bit more aggressive than C++ temporaries, as the latter are kept alive until the end of a statement, the former have no such guarantee.
cppyy sets automatic lifelines when methods return members of a temporary object, but it doesn't detect returns of pointers into that temporary. It should be easy enough to provided that for methods such as std::vector<>::begin()
.
Note that the slicing creates a new std::vector<>
, so v.begin()+2
, which indexes the original vector, is way cheaper for larger vectors.
It appears that if I create a c++ vector iterator from a python slice the memory management is a little wonky. If I don't slice and just use the python list it works great. If I use the python list and increment the iterator it also works great.
Output:
note the middle line, that's the vectorized slice. The other two are correct.