Skip to content

Commit

Permalink
Properly free IValue::tuple elements (issue LaurentMazare#123). (Laur…
Browse files Browse the repository at this point in the history
…entMazare#124)

* Properly free IValue::tuple elements.

The 'ati_deep_free' function is removed as it causes heap corruption. The IValue::to_c function has been modified to avoid the memory leaks that the 'ati_deep_free' function tried to solve.

* Coding style (else cargo fmt fails).
  • Loading branch information
svenslaggare authored and LaurentMazare committed Nov 23, 2019
1 parent fa6cef3 commit bd9da2d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/wrappers/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ impl IValue {
IValue::Double(f) => ati_double(*f),
IValue::Tuple(v) => {
let v = v.iter().map(Self::to_c).collect::<Fallible<Vec<_>>>()?;
ati_tuple(v.as_ptr(), v.len() as c_int)
let tuple = ati_tuple(v.as_ptr(), v.len() as c_int);
for x in v {
ati_free(x);
}

tuple
}
}
});
Expand Down Expand Up @@ -103,7 +108,7 @@ impl CModule {
let c_ivalue =
unsafe_torch_err!({ atm_forward_(self.c_module, ts.as_ptr(), ts.len() as c_int) });
for x in ts {
unsafe { ati_free_deep(x) }
unsafe { ati_free(x) }
}
IValue::of_c(c_ivalue)
}
Expand Down
8 changes: 0 additions & 8 deletions torch-sys/libtch/torch_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,4 @@ void ati_free(ivalue i) {
delete(i);
}

void ati_free_deep(ivalue i) {
if (i->isTuple()) {
auto vec = i->toTuple()->elements();
for (int j = 0; j < vec.size(); ++j) ati_free_deep(&vec[j]);
}
delete(i);
}

#include "torch_api_generated.cpp.h"
1 change: 0 additions & 1 deletion torch-sys/libtch/torch_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ void ati_to_tuple(ivalue, ivalue *, int);
int ati_tag(ivalue);

void ati_free(ivalue);
void ati_free_deep(ivalue);

#include "torch_api_generated.h"

Expand Down
1 change: 0 additions & 1 deletion torch-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ extern "C" {
pub fn ati_to_tuple(arg: *mut CIValue, outputs: *mut *mut CIValue, n: c_int) -> c_int;

pub fn ati_free(arg: *mut CIValue);
pub fn ati_free_deep(arg: *mut CIValue);

pub fn atm_load(filename: *const c_char) -> *mut CModule_;
pub fn atm_forward(m: *mut CModule_, args: *const *mut C_tensor, n: c_int) -> *mut C_tensor;
Expand Down

0 comments on commit bd9da2d

Please sign in to comment.