Skip to content

Commit 3eab4db

Browse files
authored
Merge pull request rust-lang#225 from Hywan/fix-module-leak
fix(module,values) Fix memory leaks
2 parents 5e64c01 + f3a9aac commit 3eab4db

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/module.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use std::mem::{forget, MaybeUninit};
2323
use std::path::Path;
2424
use std::ptr;
2525
use std::rc::Rc;
26-
use std::slice::from_raw_parts;
2726

2827
use crate::{AddressSpace, OptimizationLevel};
2928
#[llvm_versions(7.0..=latest)]
@@ -930,20 +929,18 @@ impl<'ctx> Module<'ctx> {
930929
/// ```
931930
pub fn get_global_metadata(&self, key: &str) -> Vec<MetadataValue<'ctx>> {
932931
let c_string = to_c_str(key);
933-
let count = self.get_global_metadata_size(key);
932+
let count = self.get_global_metadata_size(key) as usize;
934933

935-
let mut raw_vec: Vec<LLVMValueRef> = Vec::with_capacity(count as usize);
936-
let ptr = raw_vec.as_mut_ptr();
934+
let mut vec: Vec<LLVMValueRef> = Vec::with_capacity(count);
935+
let ptr = vec.as_mut_ptr();
937936

938-
forget(raw_vec);
939-
940-
let slice = unsafe {
937+
unsafe {
941938
LLVMGetNamedMetadataOperands(self.module.get(), c_string.as_ptr(), ptr);
942939

943-
from_raw_parts(ptr, count as usize)
940+
vec.set_len(count);
944941
};
945942

946-
slice.iter().map(|val| MetadataValue::new(*val)).collect()
943+
vec.iter().map(|val| MetadataValue::new(*val)).collect()
947944
}
948945

949946
/// Gets the first `GlobalValue` in a module.

src/values/metadata_value.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use crate::values::{BasicMetadataValueEnum, Value};
1212

1313
use std::ffi::CStr;
1414
use std::fmt;
15-
use std::mem::forget;
16-
use std::slice::from_raw_parts;
1715

1816
// TODOC: Varies by version
1917
#[cfg(feature = "llvm3-6")]
@@ -109,19 +107,19 @@ impl<'ctx> MetadataValue<'ctx> {
109107
return Vec::new();
110108
}
111109

112-
let count = self.get_node_size();
113-
let mut raw_vec: Vec<LLVMValueRef> = Vec::with_capacity(count as usize);
114-
let ptr = raw_vec.as_mut_ptr();
110+
let count = self.get_node_size() as usize;
111+
let mut vec: Vec<LLVMValueRef> = Vec::with_capacity(count);
112+
let ptr = vec.as_mut_ptr();
115113

116-
forget(raw_vec);
117-
118-
let slice = unsafe {
114+
unsafe {
119115
LLVMGetMDNodeOperands(self.as_value_ref(), ptr);
120116

121-
from_raw_parts(ptr, count as usize)
117+
vec.set_len(count)
122118
};
123119

124-
slice.iter().map(|val| BasicMetadataValueEnum::new(*val)).collect()
120+
vec.iter()
121+
.map(|val| BasicMetadataValueEnum::new(*val))
122+
.collect()
125123
}
126124

127125
pub fn print_to_string(self) -> LLVMString {

0 commit comments

Comments
 (0)