[BUG]: PyObject_Type resource leak

未关闭 适合新手
#6,312 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, python
领域
tooling

调研方向

从 include/pybind11/typing.h 第 101 行附近开始,检查 PYBIND11_OBJECT_DEFAULT(..., PyObject_Type) 对 typing::Union、Optional、Final、ClassVar、Literal 和 TypeVar 的声明,并将它们的 check_ 实现与较新的 typing::Object 进行比较。该 issue 已经指出了泄漏:PyObject_Type 返回一个新的强引用,而 check_ 将其丢弃。先阅读周围的宏定义,然后验证这些 check_ 路径不再留下无主引用,与 h.ptr() != nullptr 的方式保持一致。完成意味着参数加载不再泄漏类型引用。

由索引模型根据 Issue 内容生成。

描述

triage
Required prerequisites
What version (or hash if on master) of pybind11 are you using?

a3954715fe849519754abded4d572bf7edab67e3

Problem description

PyObject_Type returns a new strong reference to the object's type object which is never decrefed by the check or check_ functions.
https://docs.python.org/3/c-api/object.html#c.PyObject_Type

@InvincibleRMC it looks like you implemented these. What was the intent with the PyObject_Type check?

AI generated text from #6285
include/pybind11/typing.h:101: each argument load of typing::Union, Optional, Final, ClassVar, Literal or TypeVar leaks one reference to the type of the argument.

  • These classes use PYBIND11_OBJECT_DEFAULT(..., PyObject_Type), and check_ discards the new reference that PyObject_Type returns.
  • Fix: use the same check_ as the new typing::Object (h.ptr() != nullptr).
Reproducible example code

Is this a regression? Put the last known working version here if it is.

Not a regression

主要语言
C++
星标
18k
派生
2.3k
平均合并
5 天 7 小时
30 天内合并 PR
53

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pybind/pybind11 的其他 Issue

查看 pybind/pybind11 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。