[mlir] fix a memory leak in NestedPattern
NestedPattern uses a BumpPtrAllocator to store child (nested) pattern objects to decrease the overhead of dynamic allocation. This assumes all allocations happen inside the allocator that will be freed as a whole. However, NestedPattern contains `std::function` as a member, which allocates internally using `new`, unaware of the BumpPtrAllocator. Since NestedPattern only holds pointers to the nested patterns allocated in the BumpPtrAllocator, it never calls their destructors, so the destructor of the `std::function`s they contain are never called either, leaking the allocated memory. Make NestedPattern explicitly call destructors of nested patterns. This additionally requires to actually copy the nested patterns in copy-construction and copy-assignment instead of just sharing the pointer to the arena-allocated list of children to avoid double-free. An alternative solution would be to add reference counting to the list of arena-allocated list of children. Reviewed By: nicolasvasilache Differential Revision: https://reviews.llvm.org/D98485
Loading
Please register or sign in to comment