Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak using ExtractImageChips<T>(Array2DBase, IEnumerable<ChipDetails>) #17

Closed
akade opened this issue May 15, 2018 · 5 comments
Closed
Labels

Comments

@akade
Copy link

akade commented May 15, 2018

ExtractImageChips(Array2DBase, IEnumerable) leaks unmanaged memory. I use it in a realtime application every frame and end up with huge amounts of used memory that I verified to be unmanaged.

Looking at the code, I can see that Array<> does not clean up the elements (it just calls delete on the pointer). As Array<Array2D> has objects as content, I suspect this is where the memory gets leaked.

Any suggestions on how I can fix this?

@takuya-takeuchi
Copy link
Owner

That's right.
For now, array class will not be released.

DLLEXPORT void array_delete(void* obj)
{
    // dlib::array is template type.
    // Ex. dlib::array<dlib::matrix<float, 31, 1>> does NOT equal to dlib::array<dlib::matrix<float>>
    // Template argument is decided in compile time rather than runtime.
    // So we can not specify template argument as variable.
    // In other words, we have to call delete for void* because we do not known exact type.
    // Unfortunately, dlib::array implement destructor.
    // What should we do?
    delete obj;
}

However, I probably should check type and delete it for except for template types looks like dlib::array<dlib::matrix<float, 31, 1>>.

@akade
Copy link
Author

akade commented May 16, 2018

I fixed it for me by modifying the wrapper:

` protected override void DisposeUnmanaged()
{
base.DisposeUnmanaged();

        switch (_ItemType)
        {
            case ItemTypes.PixelType:
                {
                    Dlib.Native.array_delete(this.NativePtr);
                }
                break;
            case ItemTypes.Array2D:
                {
                    var type = this._ArrayElementType.ToNativeArray2DType();
                    Dlib.Native.array_array2d_delete(type, this.NativePtr);
                }
                break;
            case ItemTypes.Matrix:
                {
                    throw new NotSupportedException("Memory leak");
                }
                break;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }`

and adding the following native method:

DLLEXPORT void array_array2d_delete(array2d_type type, void* obj) { switch (type) { case array2d_type::UInt8: delete ((dlib::array<array2d<uint8_t>>*)obj); break; case array2d_type::UInt16: delete ((dlib::array<array2d<uint16_t>>*)obj); break; case array2d_type::Int16: delete ((dlib::array<array2d<int16_t>>*)obj); break; case array2d_type::Int32: delete ((dlib::array<array2d<int32_t>>*)obj); break; case array2d_type::Float: delete ((dlib::array<array2d<float>>*)obj); break; case array2d_type::Double: delete ((dlib::array<array2d<double>>*)obj); break; case array2d_type::RgbPixel: delete ((dlib::array<array2d<rgb_pixel>>*)obj); break; case array2d_type::HsiPixel: delete ((dlib::array<array2d<hsi_pixel>>*)obj); break; case array2d_type::RgbAlphaPixel: delete ((dlib::array<array2d<rgb_alpha_pixel>>*)obj); break; } }
I also did that for the matrix class, where you already had this code but as comment because of some special fixed size matrix templates. I had no issueswith it and I think at least the above array code is save.
And it fixed my memory leak. In case you want to integrate that.

@takuya-takeuchi
Copy link
Owner

Thank you so much!!
Please give me moment to check your codes.
Unfortunately, I am busy here recently, so I can not do any coding :(

@takuya-takeuchi
Copy link
Owner

OK, It should be ok.
Could you send PR if you can?
After merge, I add code to release array.

@takuya-takeuchi
Copy link
Owner

Fix by 5c34efa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants