dpilger26 / NumCpp

C++ implementation of the Python Numpy library
https://dpilger26.github.io/NumCpp
MIT License
3.51k stars 548 forks source link

Memory Leak in NdArray #207

Closed jzylkin closed 9 months ago

jzylkin commented 9 months ago

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.

dpilger26 commented 9 months ago

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;
}
jzylkin commented 9 months ago

Hi David,

I was looking at an older release from this February. I did not realize you fixed this already. Thanks for getting back to me!

Jack

On Thu, Dec 7, 2023 at 4:11 PM David Pilger @.***> wrote:

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;

}

— Reply to this email directly, view it on GitHub https://github.com/dpilger26/NumCpp/issues/207#issuecomment-1846298075, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKIXJCEUAOWRKQL4DAFNL3YIJLMFAVCNFSM6AAAAABALYLX52VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNBWGI4TQMBXGU . You are receiving this because you authored the thread.Message ID: @.***>