E2E drop check for memory leaks

This commit is contained in:
Igor Katson 2024-08-21 16:47:48 +01:00
parent 3067ad21d5
commit 2ad5fa2f12
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 53 additions and 7 deletions

View file

@ -1,8 +1,13 @@
use std::{io::Write, path::Path};
use std::{
io::Write,
path::Path,
sync::{Arc, Weak},
};
use anyhow::Context;
use axum::{response::IntoResponse, routing::get, Router};
use librqbit_core::Id20;
use parking_lot::RwLock;
use rand::{thread_rng, Rng, RngCore, SeedableRng};
use tempfile::TempDir;
use tracing::{debug, info};
@ -124,3 +129,32 @@ async fn debug_server() -> anyhow::Result<()> {
pub fn spawn_debug_server() {
tokio::spawn(debug_server());
}
pub trait DropPlaceholder: Send + Sync {}
impl<T: Send + Sync> DropPlaceholder for T {}
struct DropCheck {
obj: Weak<dyn DropPlaceholder>,
name: String,
}
#[derive(Default, Clone)]
pub struct DropChecks(Arc<RwLock<Vec<DropCheck>>>);
impl DropChecks {
pub fn add<T: DropPlaceholder + 'static, S: Into<String>>(&self, obj: &Arc<T>, name: S) {
let weak = Arc::downgrade(obj);
self.0.write().push(DropCheck {
obj: weak as Weak<dyn DropPlaceholder>,
name: name.into(),
})
}
}
impl Drop for DropCheck {
fn drop(&mut self) {
if self.obj.upgrade().is_some() {
panic!("memory leak: {}", self.name);
}
}
}