dpilger26 / NumCpp

C++ implementation of the Python Numpy library

Home Page:https://dpilger26.github.io/NumCpp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory Leak in NdArray

jzylkin opened this issue · comments

Describe the bug
The NdArray<> class can allocate memory to create the underlying _array using std:allocator, but its destructor does not deallocate this memory.

To Reproduce
Steps to reproduce the behavior:

  1. Create an NdArray object on the stack using the constructor that takes a size and shape.
  2. Observe that memory is allocated on the heap for this array.
  3. After the NdArray object goes out of scope, the memory is not freed.

Expected behavior
I expect the NdArray object to clean up the memory it creates when it is destroyed.

Not sure what you are talking about? The NdArray destructor calls deleteArray() which deallocates the heap memory.

void deleteArray() noexcept
{
    if (ownsPtr_ && array_ != nullptr)
    {
        allocator_.deallocate(array_, size_);
    }

    array_      = nullptr;
    shape_.rows = shape_.cols = 0;
    size_                     = 0;
    ownsPtr_                  = false;
    endianess_                = Endian::NATIVE;
}