Skip to content

Commit

Permalink
Merge pull request #69 from ivanallen/memleak
Browse files Browse the repository at this point in the history
fix memleak
  • Loading branch information
serizba authored Nov 25, 2020
2 parents efabf78 + 5424d5f commit 2d6f941
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
4 changes: 3 additions & 1 deletion examples/load_model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ project(example)
find_library(TENSORFLOW_LIB tensorflow HINT $ENV{HOME}/libtensorflow2/lib)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
set(CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -lasan")

add_executable(example main.cpp)
target_include_directories(example PRIVATE ../../include $ENV{HOME}/libtensorflow2/include)
target_link_libraries (example "${TENSORFLOW_LIB}")
target_link_libraries (example "${TENSORFLOW_LIB}")
5 changes: 5 additions & 0 deletions examples/load_model/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ int main() {

std::cout << output << std::endl;

auto values = output.get_data<float>();

for (auto v : values) {
std::cout << v << std::endl;
}
return 0;
}
25 changes: 25 additions & 0 deletions include/cppflow/defer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include <functional>

namespace cppflow {

class defer {
public:
typedef std::function<void ()> Func;

explicit defer(const Func& func) : _func(func) {}
~defer() {
_func();
}

defer(const defer&) = delete;
defer(defer&&) = delete;
defer& operator=(const defer&) = delete;
void* operator new (size_t) = delete;
void operator delete (void*) = delete;

private:
Func _func;
};

} // namespace cppflow
7 changes: 7 additions & 0 deletions include/cppflow/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector>

#include "context.h"
#include "defer.h"
#include "tensor.h"

namespace cppflow {
Expand Down Expand Up @@ -84,6 +85,12 @@ namespace cppflow {

std::vector<TF_Output> inp_ops(inputs.size());
std::vector<TF_Tensor*> inp_val(inputs.size());

defer d([&inp_val]{
for (auto* tf_tensor : inp_val) {
TF_DeleteTensor(tf_tensor);
}
});
for (int i=0; i<inputs.size(); i++) {

// Operations
Expand Down
9 changes: 7 additions & 2 deletions include/cppflow/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ namespace cppflow {

// EXECUTE
int n = 1;
TFE_TensorHandle* res[1];
TFE_TensorHandle* res[1] = { nullptr };
TFE_Execute(op, res, &n, context::get_status());
status_check(context::get_status());
TFE_DeleteOp(op);

tensor r;
r.tf_tensor = { TFE_TensorHandleResolve(res[0], context::get_status()), TF_DeleteTensor};
status_check(context::get_status());
TFE_DeleteTensorHandle(res[0]);

r.tfe_handle = {TFE_NewTensorHandle(r.tf_tensor.get(), context::get_status()), TFE_DeleteTensorHandle};
status_check(context::get_status());

Expand Down Expand Up @@ -205,7 +207,10 @@ namespace cppflow {

// Convert to correct type
const auto T_data = static_cast<T*>(raw_data);
return std::vector<T>(T_data, T_data + size);
std::vector<T> r(T_data, T_data + size);
TF_DeleteTensor(res_tensor);

return r;
}

datatype tensor::dtype() const {
Expand Down

0 comments on commit 2d6f941

Please sign in to comment.