Skip to content

Commit

Permalink
Memory Leak Fixes in NNTrainer
Browse files Browse the repository at this point in the history
This PR addresses memory leak issues in the NNTrainer.

**Changes proposed in this PR:**
- Release MemoryData pointer in the Tensor constructor using the vector.

**Self-evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test:   [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: Donghyeon Jeong <[email protected]>
  • Loading branch information
djeong20 authored and jijoongmoon committed Feb 7, 2025
1 parent a497055 commit a6ce493
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions nntrainer/tensor/char_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ CharTensor::CharTensor(
(void *)(new int8_t[dim.getDataLen() + sizeof(float) * scale_size()]()));
data = std::shared_ptr<MemoryData>(mem_data, [](MemoryData *mem_data) {
delete[] mem_data->getAddr<int8_t>();
delete mem_data;
});

offset = 0;
Expand Down
1 change: 1 addition & 0 deletions nntrainer/tensor/float_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class FloatTensor : public TensorBase {
new MemoryData((void *)(new float[dim.getDataLen()]()));
data = std::shared_ptr<MemoryData>(mem_data, [](MemoryData *mem_data) {
delete[] mem_data->getAddr<float>();
delete mem_data;
});

offset = 0;
Expand Down
1 change: 1 addition & 0 deletions nntrainer/tensor/half_tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class HalfTensor : public TensorBase {
new MemoryData((void *)(new _FP16[dim.getDataLen()]()));
data = std::shared_ptr<MemoryData>(mem_data, [](MemoryData *mem_data) {
delete[] mem_data->getAddr<_FP16>();
delete mem_data;
});

offset = 0;
Expand Down
1 change: 1 addition & 0 deletions nntrainer/tensor/short_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ ShortTensor::ShortTensor(
sizeof(float) / sizeof(int16_t) * scale_size()]()));
data = std::shared_ptr<MemoryData>(mem_data, [](MemoryData *mem_data) {
delete[] mem_data->getAddr<int16_t>();
delete mem_data;
});

offset = 0;
Expand Down
6 changes: 4 additions & 2 deletions nntrainer/tensor/uint_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ UIntTensor<T>::UIntTensor(
initializer = Initializer::NONE;

MemoryData *mem_data = new MemoryData((void *)(new T[dim.getDataLen()]()));
data = std::shared_ptr<MemoryData>(
mem_data, [](MemoryData *mem_data) { delete[] mem_data->getAddr<T>(); });
data = std::shared_ptr<MemoryData>(mem_data, [](MemoryData *mem_data) {
delete[] mem_data->getAddr<T>();
delete mem_data;
});

offset = 0;

Expand Down

0 comments on commit a6ce493

Please sign in to comment.