【发布时间】:2015-04-23 07:58:14
【问题描述】:
在我为Cassandra C++ driver 编写safe wrapper 的过程中,现在我的注意力转向了在调用具有以下签名的C 函数时避免内存泄漏:
cass_string_init2(const char* data, cass_size_t length);
或
cass_string_init(const char* null_terminated);
我尝试了几种名义上可行的不同方法,并产生了正确的结果,但我还没有找到一种方法来正确管理这些数据的生命周期。下面是两个示例方法。
pub fn str_to_ref(mystr:&str) -> *const i8 {unsafe{
let cstr = CString::from_slice(mystr.as_bytes());
cstr.as_slice().as_ptr()
}}
和
pub fn str_to_ref(mystr: &str) -> *const i8 {
let l = mystr.as_bytes();
unsafe {
let b = alloc::heap::allocate(mystr.len()+1, 8);
let s = slice::from_raw_parts_mut(b, mystr.len()+1);
slice::bytes::copy_memory(s, l);
s[mystr.len()] = 0;
return b as *const i8;
}
}
第一个执行无效的内存访问,例如
==26355== Address 0x782d140 is 0 bytes inside a block of size 320 free'd
==26355== at 0x1361A8: je_valgrind_freelike_block (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==26355== by 0x11272D: heap::imp::deallocate::h7b540039fbffea4dPha (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==26355== by 0x112679: heap::deallocate::h3897fed87b942253tba (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==26355== by 0x112627: vec::dealloc::h7978768019700822177 (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==26355== by 0x112074: vec::Vec$LT$T$GT$.Drop::drop::h239007174869221309 (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==26355== by 0x111F9D: collections..vec..Vec$LT$i8$GT$::glue_drop.5732::h978a83960ecb86a4 (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==26355== by 0x111F6D: std..ffi..c_str..CString::glue_drop.5729::h953a595760f34a9d (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==26355== by 0x112903: cql_ffi::helpers::str_to_ref::hef3994fa55168b90bqd (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
=
而第二个不知道何时释放其内存,导致:
==29782== 8 bytes in 1 blocks are definitely lost in loss record 1 of 115
==29782== at 0x12A5B2: je_mallocx (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==29782== by 0x1142D5: heap::imp::allocate::h3fa8a1c097e9ea53Tfa (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==29782== by 0x114221: heap::allocate::h18d191ce51ab2236gaa (in /home/tupshin/workspaces/rust/cql-ffi/target/basic)
==29782== by 0x112874: cql_ffi::helpers::str_to_ref::h5b60f207d1e31841bqd (helpers.rs:25)
使用这两种方法中的任何一种作为起点,或者完全不同的方法,我真的很感激一些关于正确方法的指导。
编辑:
Shep 的回答 完美 使用 cass_string_init 和 cass_string_init2 解决了我的问题。太感谢了。但是,我仍然不清楚将 *const i8 参数传递给其他函数,例如:
CASS_EXPORT CassError
cass_cluster_set_contact_points(CassCluster* cluster,
const char* contact_points);
它期望被传递一个对空终止字符串的引用。
基于之前适用于 CassStrings 的方法以及 CString 文档,我提出了以下建议:
pub struct ContactPoints(*const c_char);
pub trait AsContactPoints {
fn as_contact_points(&self) -> ContactPoints;
}
impl AsContactPoints for str {
fn as_contact_points(&self) -> ContactPoints {
let cstr = CString::new(self).unwrap();
let bytes = cstr.as_bytes_with_nul();
let ptr = bytes.as_ptr();
ContactPoints(ptr as *const i8)
}
}
(过多的 let 绑定只是为了确保我没有遗漏任何细微之处)
运行正常,但 valgrind 抱怨:
==22043== Invalid read of size 1
==22043== at 0x4C2E0E2: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==22043== by 0x4F8AED8: cass_cluster_set_contact_points (in /usr/local/lib/libcassandra.so.1.0.0)
==22043== by 0x11367A: cql_ffi::cluster::CassCluster::set_contact_points::h575496cbf7644b9e6oa (cluster.rs:76)
【问题讨论】:
-
Shep,再次感谢您的帮助。 :) 我想我几乎已经完成了完整驱动程序 api 的不安全部分。不过,我还缺少什么? (见编辑)
-
我已经更新了我的答案。以后请务必在 cmets 中使用 @-mention,否则人们不会看到您在与他们交谈。刚好重读了这一篇。 :-)
-
你是Rustaceans中的神。