Skip to content

Commit 0b958b0

Browse files
committed
Fix leaks: missing Drop for Endpoint & wrongly retained database in CBLReplicatorConfiguration
1 parent 9d7fd1b commit 0b958b0

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/replicator.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use std::{
2525
};
2626
use crate::{
2727
CblRef, Database, Dict, Document, Error, ListenerToken, MutableDict, Result, check_error,
28-
release, retain,
28+
release,
2929
slice::{from_str, self},
3030
c_api::{
3131
CBLListener_Remove, CBLAuth_CreatePassword, CBLAuth_CreateSession, CBLAuthenticator,
@@ -107,6 +107,14 @@ impl Clone for Endpoint {
107107
}
108108
}
109109

110+
impl Drop for Endpoint {
111+
fn drop(&mut self) {
112+
unsafe {
113+
release(self.get_ref());
114+
}
115+
}
116+
}
117+
110118
/** An opaque object representing authentication credentials for a remote server. */
111119
#[derive(Debug, PartialEq, Eq)]
112120
pub struct Authenticator {
@@ -786,7 +794,7 @@ impl Replicator {
786794
database: config
787795
.database
788796
.as_ref()
789-
.map(|d| retain(d.get_ref()))
797+
.map(|d| d.get_ref())
790798
.unwrap_or(ptr::null_mut()),
791799
endpoint: config.endpoint.get_ref(),
792800
replicatorType: config.replicator_type.clone().into(),

tests/utils.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ impl Drop for LeakChecker {
6868

6969
if self.start_instance_count != self.end_instance_count {
7070
info!("Leaks detected :-(");
71+
info!(
72+
"Instances before: {} | Instances after: {}",
73+
self.start_instance_count, self.end_instance_count
74+
);
7175
dump_instances();
7276
panic!("Memory leaks detected");
7377
// NOTE: This failure is likely to happen if the tests run multi-threaded, as happens by
@@ -170,6 +174,8 @@ impl ReplicationTwoDbsTester {
170174
) -> Self {
171175
init_logging();
172176

177+
let _leak_checker = LeakChecker::new();
178+
173179
// Create databases
174180
let tmp_dir = TempDir::new("cbl_rust").expect("create temp dir");
175181
let tmp_dir_path = tmp_dir.path();
@@ -211,7 +217,7 @@ impl ReplicationTwoDbsTester {
211217
central_database,
212218
replicator,
213219
replicator_continuous,
214-
_leak_checker: LeakChecker::new(),
220+
_leak_checker,
215221
}
216222
}
217223

@@ -301,6 +307,8 @@ impl ReplicationThreeDbsTester {
301307
) -> Self {
302308
init_logging();
303309

310+
let _leak_checker = LeakChecker::new();
311+
304312
// Create databases
305313
let tmp_dir = TempDir::new("cbl_rust").expect("create temp dir");
306314
let local_database_1_configuration = DatabaseConfiguration {
@@ -362,7 +370,7 @@ impl ReplicationThreeDbsTester {
362370
replicator_1_continuous,
363371
replicator_2,
364372
replicator_2_continuous,
365-
_leak_checker: LeakChecker::new(),
373+
_leak_checker,
366374
}
367375
}
368376

0 commit comments

Comments
 (0)