From ab2ddd79a11131ec1bd4e145a86301551768a80a Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Mon, 27 Jan 2025 09:24:01 +0100 Subject: [PATCH 1/3] Don't print too many "leaked instance" messages. Most terminals don't have 10k line s of scrollback buffer ... --- src/nb_internals.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/nb_internals.cpp b/src/nb_internals.cpp index b4a3f1d9..24e01e8e 100644 --- a/src/nb_internals.cpp +++ b/src/nb_internals.cpp @@ -278,15 +278,21 @@ static void internals_cleanup() { fprintf(stderr, " - leaked instance %p of type \"%s\"\n", k, tp->name); }; - for (size_t i = 0; i < p->shard_count; ++i) { + int ctr = 0; + for (size_t i = 0; i < p->shard_count && ctr < 20; ++i) { for (auto [k, v]: p->shards[i].inst_c2p) { if (NB_UNLIKELY(nb_is_seq(v))) { nb_inst_seq* seq = nb_get_seq(v); - for(; seq != nullptr; seq = seq->next) + for(; seq != nullptr && ctr < 20; seq = seq->next) { print_leak(k, seq->inst); + ctr += 1; + } } else { print_leak(k, (PyObject*)v); + ctr += 1; } + if (ctr >= 20) + break; } } #endif From 413745c53f46310771e917c0d9d12a6be82e262c Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Wed, 29 Jan 2025 14:49:03 +0100 Subject: [PATCH 2/3] Added a "skipped remainder" message --- src/nb_internals.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/nb_internals.cpp b/src/nb_internals.cpp index 24e01e8e..2b676884 100644 --- a/src/nb_internals.cpp +++ b/src/nb_internals.cpp @@ -295,6 +295,9 @@ static void internals_cleanup() { break; } } + if (ctr >= 20) { + fprintf(stderr, " - ... skipped remainder\n"); + } #endif } From a9fd096aed688f45607f4f47683cc67b3a318e67 Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Wed, 29 Jan 2025 14:56:20 +0100 Subject: [PATCH 3/3] Don't skip leak records when debugging --- src/nb_internals.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/nb_internals.cpp b/src/nb_internals.cpp index 2b676884..33f4c09e 100644 --- a/src/nb_internals.cpp +++ b/src/nb_internals.cpp @@ -267,6 +267,14 @@ static void internals_cleanup() { keep_alive_leaks += s.keep_alive.size(); } +#ifdef _DEBUG +// in debug mode, show all leak records +#define INC_CTR do {} while(0) +#else +// otherwise show just the first 10 or 20 +#define INC_CTR ctr++ +#endif + bool leak = inst_leaks > 0 || keep_alive_leaks > 0; if (print_leak_warnings && inst_leaks > 0) { @@ -285,11 +293,11 @@ static void internals_cleanup() { nb_inst_seq* seq = nb_get_seq(v); for(; seq != nullptr && ctr < 20; seq = seq->next) { print_leak(k, seq->inst); - ctr += 1; + INC_CTR; } } else { print_leak(k, (PyObject*)v); - ctr += 1; + INC_CTR; } if (ctr >= 20) break; @@ -318,7 +326,8 @@ static void internals_cleanup() { int ctr = 0; for (const auto &kv : p->type_c2p_slow) { fprintf(stderr, " - leaked type \"%s\"\n", kv.second->name); - if (ctr++ == 10) { + INC_CTR; + if (ctr == 10) { fprintf(stderr, " - ... skipped remainder\n"); break; } @@ -335,7 +344,8 @@ static void internals_cleanup() { for (auto [f, p2] : p->funcs) { fprintf(stderr, " - leaked function \"%s\"\n", nb_func_data(f)->name); - if (ctr++ == 10) { + if (ctr == 10) { + INC_CTR; fprintf(stderr, " - ... skipped remainder\n"); break; }