Skip to content

Commit 84e6b77

Browse files
committed
Sora オブジェクトに tp_traverse をつける(リークするけど)
1 parent 25536de commit 84e6b77

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

src/sora.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ std::shared_ptr<SoraConnection> Sora::CreateConnection(
208208
if (video_frame_transformer) {
209209
conn->SetVideoSenderFrameTransformer(video_frame_transformer);
210210
}
211+
212+
weak_connections_.erase(
213+
std::remove_if(
214+
weak_connections_.begin(), weak_connections_.end(),
215+
[](std::weak_ptr<SoraConnection> w) { return w.expired(); }),
216+
weak_connections_.end());
217+
weak_connections_.push_back(conn);
218+
211219
return conn;
212220
}
213221

src/sora.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ class Sora : public DisposePublisher {
159159
*/
160160
SoraVideoSource* CreateVideoSource();
161161

162+
std::vector<std::weak_ptr<SoraConnection>> weak_connections_;
163+
162164
private:
163165
/**
164166
* Python で渡された値を boost::json::value に変換します。

src/sora_sdk_ext.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,38 @@ PyType_Slot connection_slots[] = {
288288
{Py_tp_clear, (void*)connection_tp_clear},
289289
{0, nullptr}};
290290

291+
int sora_tp_traverse(PyObject* self, visitproc visit, void* arg) {
292+
if (!nb::inst_ready(self)) {
293+
return 0;
294+
}
295+
296+
Sora* sora = nb::inst_ptr<Sora>(self);
297+
for (auto wc : sora->weak_connections_) {
298+
auto conn = wc.lock();
299+
if (conn) {
300+
nb::object conn_obj = nb::find(conn);
301+
Py_VISIT(conn_obj.ptr());
302+
}
303+
}
304+
305+
return 0;
306+
}
307+
308+
int sora_tp_clear(PyObject* self) {
309+
if (!nb::inst_ready(self)) {
310+
return 0;
311+
}
312+
313+
Sora* sora = nb::inst_ptr<Sora>(self);
314+
sora->weak_connections_.clear();
315+
316+
return 0;
317+
}
318+
319+
PyType_Slot sora_slots[] = {{Py_tp_traverse, (void*)sora_tp_traverse},
320+
{Py_tp_clear, (void*)sora_tp_clear},
321+
{0, nullptr}};
322+
291323
/**
292324
* Python で利用するすべてのクラスと定数は以下のように定義しなければならない
293325
*/
@@ -529,7 +561,7 @@ NB_MODULE(sora_sdk_ext, m) {
529561
.def("__del__", &SoraVideoFrameTransformer::Del)
530562
.def_rw("on_transform", &SoraVideoFrameTransformer::on_transform_);
531563

532-
nb::class_<Sora>(m, "Sora")
564+
nb::class_<Sora>(m, "Sora", nb::type_slots(sora_slots))
533565
.def(nb::init<std::optional<bool>, std::optional<std::string>>(),
534566
"use_hardware_encoder"_a = nb::none(), "openh264"_a = nb::none())
535567
.def("create_connection", &Sora::CreateConnection, "signaling_urls"_a,

test_with_llvm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def test(debugger, command, result, internal_dict):
88
debugger.HandleCommand("settings set target.process.follow-fork-mode child")
99

1010
target = debugger.CreateTargetWithFileAndArch("uv", lldb.LLDB_ARCH_DEFAULT)
11-
process = target.LaunchSimple(["run", "pytest", "tests", "-s"], None, None)
11+
process = target.LaunchSimple(
12+
["run", "pytest", "tests/test_sora_disconnect.py", "-s"], None, None
13+
)
1214

1315
if not process:
1416
print("Error: could not launch process")

0 commit comments

Comments
 (0)