fix: potential memleak (#10955)

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou
2025-02-28 12:14:40 +08:00
committed by GitHub
parent 0d919157c9
commit 41cd375e3c
2 changed files with 45 additions and 28 deletions

View File

@@ -10,7 +10,7 @@ use crate::{
send_data, ClipboardFile, CliprdrError, CliprdrServiceContext, ProgressPercent, send_data, ClipboardFile, CliprdrError, CliprdrServiceContext, ProgressPercent,
}; };
use hbb_common::{allow_err, bail, log, ResultType}; use hbb_common::{allow_err, bail, log, ResultType};
use objc2::{msg_send_id, rc::Id, runtime::ProtocolObject, ClassType}; use objc2::{msg_send_id, rc::autoreleasepool, rc::Id, runtime::ProtocolObject, ClassType};
use objc2_app_kit::{NSPasteboard, NSPasteboardTypeFileURL}; use objc2_app_kit::{NSPasteboard, NSPasteboardTypeFileURL};
use objc2_foundation::{NSArray, NSString}; use objc2_foundation::{NSArray, NSString};
use std::{ use std::{
@@ -262,6 +262,21 @@ impl PasteboardContext {
}); });
}; };
autoreleasepool(|_| self.set_clipboard_item(tx_handle, conn_id, file_descriptor_id))?;
} else {
return Err(CliprdrError::CommonError {
description: "pasteboard context is not inited".to_string(),
});
}
Ok(())
}
fn set_clipboard_item(
&self,
tx_handle: &ContextInfo,
conn_id: i32,
file_descriptor_id: i32,
) -> Result<(), CliprdrError> {
let tx = tx_handle.tx.clone(); let tx = tx_handle.tx.clone();
let provider = create_pasteboard_file_url_provider( let provider = create_pasteboard_file_url_provider(
PasteObserverInfo { PasteObserverInfo {
@@ -288,11 +303,6 @@ impl PasteboardContext {
}); });
} }
} }
} else {
return Err(CliprdrError::CommonError {
description: "pasteboard context is not inited".to_string(),
});
}
Ok(()) Ok(())
} }
@@ -427,6 +437,7 @@ mod tests {
fn test_temp_files_count() { fn test_temp_files_count() {
let mut c = super::PasteboardContext::temp_files_count(); let mut c = super::PasteboardContext::temp_files_count();
let mut created_files = vec![];
for _ in 0..10 { for _ in 0..10 {
let path = format!( let path = format!(
"/tmp/{}{}", "/tmp/{}{}",
@@ -434,10 +445,16 @@ mod tests {
uuid::Uuid::new_v4().to_string() uuid::Uuid::new_v4().to_string()
); );
if std::fs::File::create(&path).is_ok() { if std::fs::File::create(&path).is_ok() {
created_files.push(path);
c += 1; c += 1;
} }
} }
assert_eq!(c, super::PasteboardContext::temp_files_count()); assert_eq!(c, super::PasteboardContext::temp_files_count());
// Clean up the created files.
for file in created_files {
std::fs::remove_file(&file).ok();
}
} }
} }