From a353eb98bdd262eb0ae82bdd09f805356bef37cb Mon Sep 17 00:00:00 2001 From: Till Hartmann Date: Mon, 15 May 2023 09:41:15 +0200 Subject: [PATCH 1/2] implement Drop for faidx::Reader, destroying the fai handle --- src/faidx/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/faidx/mod.rs b/src/faidx/mod.rs index a1f43e894..a38afcebf 100644 --- a/src/faidx/mod.rs +++ b/src/faidx/mod.rs @@ -128,6 +128,14 @@ impl Reader { } } +impl Drop for Reader { + fn drop(&mut self) { + unsafe { + htslib::fai_destroy(self.inner); + } + } +} + #[cfg(test)] mod tests { use super::*; From 4cdeb9672adb4cec8769af39319a1156ba279933 Mon Sep 17 00:00:00 2001 From: Till Hartmann Date: Mon, 15 May 2023 10:10:06 +0200 Subject: [PATCH 2/2] add testcase for creating and instantly dropping many faidx readers --- src/faidx/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/faidx/mod.rs b/src/faidx/mod.rs index a38afcebf..838fdc810 100644 --- a/src/faidx/mod.rs +++ b/src/faidx/mod.rs @@ -246,4 +246,12 @@ mod tests { let n = r.seq_name(1).unwrap(); assert_eq!(n, "chr2"); } + + #[test] + fn open_many_readers() { + for _ in 0..500_000 { + let reader = open_reader(); + drop(reader); + } + } }