Skip to content

Commit 8ac430d

Browse files
committed
fix memory leaks
1 parent ee4153a commit 8ac430d

File tree

1 file changed

+18
-32
lines changed

1 file changed

+18
-32
lines changed

src/evaluator.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,13 @@ impl Type {
7676

7777
pub fn obj2str(sc: *mut s7_scheme, obj: *mut s7_cell) -> String {
7878
unsafe {
79-
unsafe {
80-
let expr = s7_object_to_c_string(sc, obj);
81-
let cstr = CStr::from_ptr(expr);
82-
let result = match cstr.to_str() {
83-
Ok(expr) => expr.to_owned(),
84-
Err(_) => format!("(error 'encoding-error \"Failed to encode string\")"),
85-
};
86-
free(expr as *mut libc::c_void);
87-
result
88-
}
79+
let expr = s7_string(s7_object_to_string(sc, obj, false));
80+
let cstr = CStr::from_ptr(expr);
81+
let result = match cstr.to_str() {
82+
Ok(expr) => expr.to_owned(),
83+
Err(_) => format!("(error 'encoding-error \"Failed to encode string\")"),
84+
};
85+
result
8986
}
9087
}
9188

@@ -108,24 +105,21 @@ pub fn scheme2json(expression: &str) -> Result<Value, String> {
108105
// - @hash-table: {"*type/hash-table*": [["a", 6], [53, 199]]}
109106

110107
unsafe {
111-
unsafe {
112-
let sc: *mut s7_scheme = s7_init();
108+
let sc: *mut s7_scheme = s7_init();
113109

114-
// Parse the expression without evaluating it
115-
let c_expr = CString::new(expression).unwrap_or_else(|_| CString::new("()").unwrap());
116-
let input_port = s7_open_input_string(sc, c_expr.as_ptr());
117-
let s7_obj = s7_read(sc, input_port);
118-
s7_close_input_port(sc, input_port);
110+
// Parse the expression without evaluating it
111+
let c_expr = CString::new(expression).unwrap_or_else(|_| CString::new("()").unwrap());
112+
let input_port = s7_open_input_string(sc, c_expr.as_ptr());
113+
let s7_obj = s7_read(sc, input_port);
114+
s7_close_input_port(sc, input_port);
119115

120-
let result = s7_obj_to_json(sc, s7_obj);
121-
s7_free(sc);
122-
result
123-
}
116+
let result = s7_obj_to_json(sc, s7_obj);
117+
s7_free(sc);
118+
result
124119
}
125120
}
126121

127122
pub fn json2scheme(expression: Value) -> Result<String, String> {
128-
unsafe {
129123
unsafe {
130124
let sc: *mut s7_scheme = s7_init();
131125
match json_to_s7_obj(sc, &expression) {
@@ -139,7 +133,6 @@ pub fn json2scheme(expression: Value) -> Result<String, String> {
139133
Err(err)
140134
}
141135
}
142-
}
143136
}
144137
}
145138

@@ -162,7 +155,6 @@ impl Evaluator {
162155

163156
primitives_.extend(primitives);
164157

165-
unsafe {
166158
unsafe {
167159
let sc: *mut s7_scheme = s7_init();
168160

@@ -209,7 +201,6 @@ impl Evaluator {
209201
sc,
210202
primitives: primitives_,
211203
}
212-
}
213204
}
214205
}
215206

@@ -232,19 +223,17 @@ impl Evaluator {
232223

233224
impl Drop for Evaluator {
234225
fn drop(&mut self) {
235-
unsafe {
236226
unsafe {
237227
s7_free(self.sc);
238228
}
239-
}
240229
}
241230
}
242231

243232
fn primitive_expression_to_byte_vector() -> Primitive {
244233
unsafe extern "C" fn code(sc: *mut s7_scheme, args: s7_pointer) -> s7_pointer {
245234
let arg = s7_car(args);
246235

247-
let s7_c_str = s7_object_to_c_string(sc, arg);
236+
let s7_c_str = s7_string(s7_object_to_string(sc, arg, false));
248237
let c_string = CStr::from_ptr(s7_c_str);
249238

250239
let bv = s7_make_byte_vector(
@@ -256,7 +245,6 @@ fn primitive_expression_to_byte_vector() -> Primitive {
256245
for (i, b) in c_string.to_bytes().iter().enumerate() {
257246
s7_byte_vector_set(bv, i as i64, *b);
258247
}
259-
free(s7_c_str as *mut libc::c_void);
260248
bv
261249
}
262250

@@ -328,7 +316,7 @@ fn primitive_hex_string_to_byte_vector() -> Primitive {
328316
);
329317
}
330318

331-
let s7_c_str = s7_object_to_c_string(sc, arg);
319+
let s7_c_str = s7_string(s7_object_to_string(sc, arg, false));
332320
let hex_string = CStr::from_ptr(s7_c_str)
333321
.to_str()
334322
.expect("Failed to convert C string to hex string");
@@ -338,8 +326,6 @@ fn primitive_hex_string_to_byte_vector() -> Primitive {
338326
.map(|i| u8::from_str_radix(&hex_string[i..i + 2], 16))
339327
.collect();
340328

341-
free(s7_c_str as *mut libc::c_void);
342-
343329
match result {
344330
Ok(result) => {
345331
let bv =

0 commit comments

Comments
 (0)