free unused memory / fix memory leak
This commit is contained in:
parent
91bf2c0ea8
commit
a2a977b75c
2 changed files with 9 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue