1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

free unused memory / fix memory leak

This commit is contained in:
sdeml 2019-01-18 21:05:40 +01:00
parent 91bf2c0ea8
commit a2a977b75c
2 changed files with 9 additions and 1 deletions

View file

@ -15,6 +15,11 @@ extern "C" {
char* _sha256RoundConstraints();
char* _sha256RoundWitness(const uint8_t* input, int input_length);
// External interface to free memory
void _free_string(char const *str) {
delete[] str;
}
#ifdef __cplusplus
} // extern "C"
#endif

View file

@ -15,13 +15,15 @@ use zokrates_field::field::Field;
extern "C" {
fn _sha256RoundConstraints() -> *mut c_char;
fn _sha256RoundWitness(inputs: *const uint8_t, inputs_length: c_int) -> *mut c_char;
fn _free_string(str: *const c_char);
}
pub fn get_sha256round_constraints() -> String {
let c_buf: *const c_char = unsafe { _sha256RoundConstraints() };
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };
let str_slice: &str = c_str.to_str().unwrap();
let str_buf: String = str_slice.to_owned();
let str_buf: String = str_slice.to_owned(); //memory allocated in Rust
unsafe { _free_string(c_buf) }; //memory deallocated in Cpp
str_buf
}
@ -35,6 +37,7 @@ pub fn get_sha256round_witness<T: Field>(inputs: &Vec<T>) -> String {
let c_str: &CStr = unsafe { CStr::from_ptr(c_buf) };
let str_slice: &str = c_str.to_str().unwrap();
let str_buf: String = str_slice.to_owned();
unsafe { _free_string(c_buf) };
str_buf
}