pybind / pybind/pybind11

[BUG] Ref count of numpy array inside list is wrong when compiling with the latest standard on Microsoft Visual Studio

Open
#3,118 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Issue description

Ref count of numpy array is wrong and causes a leak when compiled in Visual Studio with the standard set to latest. It is fine when set so C++ 17.

Development environment : Windows 10
Visual Studio Version 16.9.5
Intel® C++ Compiler Package ID: w_comp_lib_2020.1.216
Python version is 3.8.6
numpy version is 1.21

Reproducible example code

//binding.cpp
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>

namespace py = pybind11;

void function_that_does_nothing(std::vector<py::array_t<float>>& images)
{
    return;
}

PYBIND11_MODULE(Project1, m) {
    m.def("function_that_does_nothing", &function_that_does_nothing);
}

//main.py
import sys
import Project1 as project
import numpy as np


something_to_process = []
arr = np.ones((3008, 4112))
something_to_process.append(arr)
project.function_that_does_nothing(something_to_process)
print(f"The ref count ----> {sys.getrefcount(something_to_process[0])}")

The Visual Studio option of interest is under Project -> Properties -> General ->C++ Language Standard. To see the issue, you can try compiling with the option set to Preview - Features from the Latest C++ Working Draft (/std:c++latest) then set to ISO C++17 Standard (/std:c++17).

Thank you

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the binding.cpp and main.py reproducer, compiling it in Visual Studio with /std:c++latest and /std:c++17. Compare the reported reference count and leak behavior for the numpy array passed inside the list. Done means the latest standard no longer produces an incorrect reference count or leak while the C++17 behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, numpy, python
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.