Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes memory leak due to sequences allocated by minimap2 not being freed #59

Merged
merged 2 commits into from
Jul 14, 2024

Conversation

charlesgregory
Copy link
Contributor

Switches mm_idx from being a struct back to a pointer for Aligner struct.

When rust takes ownership here, it doesn't actually get ownership since it just does a Copy of the struct but the data allocated by minimap2 still exists.

self.idx = Some(unsafe { *idx.assume_init() });

Even if you were to free the original pointer with mm_idx_destroy after that line, it would later segfault since idx.seq is just a pointer to the sequences allocated by minimap2 here:
https://github.com/lh3/minimap2/blob/69e36299168d739dded1c5549f662793af10da83/index.c#L425

So I changed the struct back to a pointer and used the manual Drop impl to free the minimap2 index.

Thanks for the crate btw!

Switches mm_idx from being a struct back to a pointer for Aligner struct
@jguhlin
Copy link
Owner

jguhlin commented Jun 19, 2024

Awesome! It's my first ffi project so I knew there were memory leaks I've missed. I'm out of town so will get merged early Juy.

@jianshu93
Copy link

I can compile it but just found that the import is not necessary:

warning: unused import: std::ops::DerefMut
--> src/lib.rs:51:5
|
51 | use std::ops::DerefMut;
| ^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_imports)] on by default

warning: minimap2 (lib) generated 1 warning (run cargo fix --lib -p minimap2 to apply 1 suggestion)

Other looks good.

Jianshu

@jianshu93
Copy link

hi @jguhlin, it would be nice if you can merge this to main and publish a new release on crates.io. I cannot wait to use the memory safe version.

Thanks!

Jianshu

@jguhlin
Copy link
Owner

jguhlin commented Jul 13, 2024

@jianshu93 Hey, it's Sat here, but I'll get this on Monday. Been deep in grant writing and other work since I've been back. Cheers

@jguhlin jguhlin merged commit 75e72cf into jguhlin:main Jul 14, 2024
0 of 2 checks passed
jguhlin added a commit that referenced this pull request Jul 14, 2024
@jguhlin
Copy link
Owner

jguhlin commented Jul 14, 2024

@charlesgregory @jianshu93 Released in 0.1.19

@jianshu93
Copy link

jianshu93 commented Jul 14, 2024

Many thanks! this is so fast! Tests passed and I will rely on it for several softwares, e.g., ANI calculation, overlap detection.

Jianshu

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants